Java Reference
In-Depth Information
System
.
out
.
println
(
"Creating Directory: "
+
dirName
);
iif
(!
d
.
mkdirs
()) {
System
.
err
.
println
(
"Warning: unable to mkdir "
+
dirName
);
}
dirsMade
.
add
(
dirName
);
}
}
}
System
.
err
.
println
(
"Creating "
+
zipName
);
FileOutputStream os
=
new
new
FileOutputStream
(
zipName
);
InputStream is
=
zippy
.
getInputStream
(
e
);
int
int
n
=
0
;
while
while
((
n
=
is
.
read
(
b
)) >
0
)
os
.
write
(
b
,
0
,
n
);
is
.
close
();
os
.
close
();
break
break
;
case
case
LIST:
// Not extracting, just list
iif
(
e
.
isDirectory
()) {
System
.
out
.
println
(
"Directory "
+
zipName
);
}
else
else
{
System
.
out
.
println
(
"File "
+
zipName
);
}
break
break
;
default
default
:
throw
throw new
new
IllegalStateException
(
"mode value ("
+
mode
+
") bad"
);
}
}
}
Finding Files in a Filesystem-Neutral Way with
getResource() and getResourceAsStream()
Problem
You want to load objects or files without referring to their absolute location in the filesystem.
This can be because you are in a server (Java EE) environment, or just because you want to
be independent of file paths, or because you expect users to deploy the resource “some-
where” on the classpath (possibly even inside a JAR file).