> -----Original Message-----
> From: KARR, DAVID
> Sent: Friday, March 13, 2015 10:02 AM
> To: Commons Users List
> Subject: [commons-io] FileUtils.openOutputStream(file) doesn't create the
> file
>
> 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?
After more investigation, it seems like this isn't really a problem with
FileUtils. The FileOutputStream javadoc does say somewhat indirectly that it
will create the file if it doesn't exist, so this should have worked.
I even added the following code right before my call to
"FileUtils.openOutputStream()":
-------------------
if (!file.exists()) {
logger.info("Parent file exists: " +
file.getParentFile().exists());
try {
file.createNewFile();
}
catch (Exception ex) {
logger.error("Creating file failed", ex);
}
}
--------------
This prints "true" for the parent file, and then reports "java.io.IOException:
The system cannot find the path specified" on the "createNewFile()" call.
I did note that the file path I'm creating is quite long, but it's still well
short of 260 characters (about 230), the supposed limit of file paths in
Windows.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]