commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "KARR, DAVID" <>
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: .... (The system cannot find the path specified)

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:
For additional commands, e-mail:

View raw message