FileOutputStream Does Not Create File?

J

Jason Cavett

I'm trying to extract a ZIP file to a temporary directory
(System.getProperty("java.io.tmpdir")) and I keep getting the
following exception when I get to this line:

FileOutputStream fos = new FileOutputStream(TEMP_DIR +
entry.getName());

The exception is:

java.io.FileNotFoundException: C:\data\myusername\Desktop\zipped.txt
(The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at hasrd.gui.skin.ThemeLoader.loadTheme(ThemeLoader.java:82)
at hasrd.gui.skin.ThemeLoader.loadTheme(ThemeLoader.java:57)
at hasrd.TestZipInput.main(TestZipInput.java:11)

Can a FileOutputStream create a directory if it does not already
exist? Is there something else I'm doing wrong?


Thanks
 
T

Thomas Kellerer

Jason Cavett wrote on 25.04.2008 16:24:
Can a FileOutputStream create a directory if it does not already
exist?
No. You need to use a File object to first create the directory, e.g. File.mkdirs()

Thomas
 
L

Leonard Milcin

Jason said:
I'm trying to extract a ZIP file to a temporary directory
(System.getProperty("java.io.tmpdir")) and I keep getting the
following exception when I get to this line:

FileOutputStream fos = new FileOutputStream(TEMP_DIR +
entry.getName());

The exception is:

java.io.FileNotFoundException: C:\data\myusername\Desktop\zipped.txt
(The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at hasrd.gui.skin.ThemeLoader.loadTheme(ThemeLoader.java:82)
at hasrd.gui.skin.ThemeLoader.loadTheme(ThemeLoader.java:57)
at hasrd.TestZipInput.main(TestZipInput.java:11)

Can a FileOutputStream create a directory if it does not already
exist?

No, it won't create it. It's your responsibility.
Is there something else I'm doing wrong?

Yes. You should create File object and pass it as a parameter to the
constructor of FileOutputStream. You can use mkdirs
http://java.sun.com/javase/6/docs/api/java/io/File.html#mkdirs() to
create the directory.

And third thing you're getting wrong. You should really consider reading
documentation;-)

Regards,
Leonard
 
J

Jason Cavett

No, it won't create it. It's your responsibility.


Yes. You should create File object and pass it as a parameter to the
constructor of FileOutputStream. You can use mkdirshttp://java.sun.com/javase/6/docs/api/java/io/File.html#mkdirs() to
create the directory.

And third thing you're getting wrong. You should really consider reading
documentation;-)

Regards,
Leonard

Actually, the issue was not as simple as what you suggested.

The problem is, I'm attempting to unzip a ZIP file. I'm following the
tutorial found here: http://java.sun.com/developer/technicalArticles/Programming/compression/
and it works, except when there is a "path" value associated with the
entry.

This is what's causing the sticking point, and I'm not really sure how
to handle it. I suppose I could parse the file name off the end of
the path (which I get through entry.getName()) and then create the
files using File.mkdirs(), but that seems too roundabout.

Is there something I'm missing at this point? Like I said - it works
when there is no path value associated with the ZIP file.


Thanks
 
L

Leonard Milcin

Jason said:
Actually, the issue was not as simple as what you suggested.

The problem is, I'm attempting to unzip a ZIP file. I'm following the
tutorial found here: http://java.sun.com/developer/technicalArticles/Programming/compression/
and it works, except when there is a "path" value associated with the
entry.

This is what's causing the sticking point, and I'm not really sure how
to handle it. I suppose I could parse the file name off the end of
the path (which I get through entry.getName()) and then create the
files using File.mkdirs(), but that seems too roundabout.

You can't create file in a directory that doesn't exist. It has to be
created before you can create a file in it. File.mkdirs() is simplest
and for most cases best way to do it. And you can use File to parse
abstract paths (eg. to get the name of directory to be created).
Is there something I'm missing at this point? Like I said - it works
when there is no path value associated with the ZIP file.

Because, then, you create the file in current directory.

Regards,
Leonard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top