What is the most succinct way to create a directory called "Foo" underneath the current working directory of my Java application (if it does not already exist)?
Or, a slightly different angle: What is the Java equivalent of Directory.CreateDirectory("Foo")
in .NET?
The java.io package does not have a Directory
class, but you can use the mkdir()
method on the File
class instead:
(new File("Foo")).mkdir()
Note that mkdir()
has two separate failure modes:
checkWrite()
method does not permit the named directory to be created" then a SecurityException
will be thrown.mkdir()
will return false. (More specifically, it will return true if and only if the directory was created.)Point 1 is ok — if you don't have permission, throw. Point 2 is a little sub-optimal for three reasons:
Aside: Contrast Point 3 with the behaviour of the .NET
Directory.CreateDirectory()
which does nothing if the directory exists. This kind of makes sense — "create a directory"; "ok, the directory is created". Does it matter if it was created now or earlier; by this process or another? If you really cared about that wouldn't you be asking a different question: "Does this directory exist?"
The next caveat is that mkdir()
will not create more than one directory at a time. For my simple example of a directory named "Foo" this is fine; however, if you wanted to create a directory called Bar within the directory Foo (i.e. to create the directory "Foo/Bar") you must remember to use the mkdirs()
method instead.
So to work around all of these caveats, you can employ a helper method such as the following:
public static File createDirectory(String directoryPath) throws IOException {
File dir = new File(directoryPath);
if (dir.exists()) {
return dir;
}
if (dir.mkdirs()) {
return dir;
}
throw new IOException("Failed to create directory '" + dir.getAbsolutePath() + "' for an unknown reason.");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With