commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "KARR, DAVID" <dk0...@att.com>
Subject [commons-io] FileUtils.openOutputStream(file) doesn't create the file
Date Fri, 13 Mar 2015 17:02:19 GMT
I'm confused by the FileUtils.openOutputStream(File) method in commons-io.

The javadoc for this method says this: "The file will be created if it does not exist."  That
doesn't seem ambiguous.  However, I noticed while stepping through new code that is calling
this for a path that doesn't exist yet, I find that the parent directory has been created,
but it's not creating the file.

In fact, it's even getting this exception:

Caused by: java.io.FileNotFoundException: .... (The system cannot find the path specified)
	at org.apache.commons.io.FileUtils.openOutputStream(FileUtils.java:367)

The implementation of this method makes it pretty clear:
----------------------
    public static FileOutputStream openOutputStream(File file, boolean append) throws IOException
{
        if (file.exists()) {
            if (file.isDirectory()) {
                throw new IOException("File '" + file + "' exists but is a directory");
            }
            if (file.canWrite() == false) {
                throw new IOException("File '" + file + "' cannot be written to");
            }
        } else {
            File parent = file.getParentFile();
            if (parent != null) {
                if (!parent.mkdirs() && !parent.isDirectory()) {
                    throw new IOException("Directory '" + parent + "' could not be created");
                }
            }
        }
        return new FileOutputStream(file, append);
    }
-----------------

The javadoc for the FileOutputStream constructor states that it will throw that exception
if the file doesn't exist.

What's going on here?



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message