Java 8+ version:
import java.nio.file.Paths;
import java.nio.file.Files;
Files.createDirectories(Paths.get("/Your/Path/Here"));
The Files.createDirectories() creates a new directory and parent directories that do not exist. This method does not throw an exception if the directory already exists.
Videos
Using java.nio.file.Files:
Path path = ...;
if (Files.exists(path)) {
// ...
}
You can optionally pass this method LinkOption values:
if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
There's also a method notExists:
if (Files.notExists(path)) {
Quite simple:
new File("/Path/To/File/or/Directory").exists();
And if you want to be certain it is a directory:
File f = new File("/Path/To/File/or/Directory");
if (f.exists() && f.isDirectory()) {
...
}
!Files.exists() returns:
trueif the file does not exist or its existence cannot be determinedfalseif the file exists
Files.notExists() returns:
trueif the file does not existfalseif the file exists or its existence cannot be determined
As we see from Files.exists the return result is:
TRUE if the file exists;
FALSE if the file does not exist or its existence cannot be determined.
And from Files.notExists the return result is:
TRUE if the file does not exist;
FALSE if the file exists or its existence cannot be determined
So if !Files.exists(path) return TRUE means it not exists or the existence cannot be determined (2 possibilities) and for Files.notExists(path) return TRUE means it not exists (just 1 possibility).
The conclusion !Files.exists(path) != Files.notExists(path) or 2 possibilities not equals to 1 possibility (see the explanation above about the possibilities).