Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FileOutputStream Does Not Create File?

3 views
Skip to first unread message

Jason Cavett

unread,
Apr 25, 2008, 10:24:59 AM4/25/08
to
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

Thomas Kellerer

unread,
Apr 25, 2008, 11:04:28 AM4/25/08
to
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

Leonard Milcin

unread,
Apr 25, 2008, 11:09:05 AM4/25/08
to

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

--
Simplicity is the ultimate sophistication.
-- Leonardo da Vinci

Jason Cavett

unread,
Apr 25, 2008, 11:29:35 AM4/25/08
to
On Apr 25, 11:09 am, Leonard Milcin <leon...@milcin.dont-spam.pl>
wrote:

> Jason Cavett wrote:
> > 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 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
>
> --
> Simplicity is the ultimate sophistication.
>                                   -- Leonardo da Vinci

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

Leonard Milcin

unread,
Apr 25, 2008, 11:42:11 AM4/25/08
to
Jason Cavett wrote:
> 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.

0 new messages