javaapache-commonsapache-commons-io

Why use FileUtils.forceMkdir?


What is the use of FileUtils.forceMkdir from Apache commons-io instead of File.mkdirs built into the JRE?

The docs to forceMkdir are

Makes a directory, including any necessary but nonexistent parent directories. If a file already exists with specified name but it is not a directory then an IOException is thrown. If the directory cannot be created (or does not already exist) then an IOException is thrown.

While the docs to mkdirs are

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

Is the only difference in the type of exception thrown?


Solution

  • Looks like the main difference is that implementation from Apache commons is "louder": it will throw exception if the directory to create already exist, but it is actually a file. Looks like the idea was to wrap the JRE implementation that returns false instead of exception in case of unsuccessful directory creation.