The article directories

      • 1. Traditional API folder creation method
      • 2. Java NIO create folder
        • 2.1.files. createDirectory Creates a folder
        • 2.2 Files. CreateDirectories creating folders and its parent folder
      • summary

1. Traditional API folder creation method

Java’s traditional IO API uses the file.mkdir() and file.mkdirs() methods in the java.io.File class to create folders

File.mkdir () returns true on success, false on failure. Also returns false if the parent folder of the created folder does not exist. No exceptions are thrown. File.mkdirs () creates a folder with its parent folder, returning true on success and false on failure. Again, no exception is thrown for failed creation.

@Test
void testCreateDir1() {// "D:\data111" directory now does not have String dirStr ="D:\\data111\\test";
   File directory = new File(dirStr);

   //mkdir
   boolean hasSucceeded = directory.mkdir();
   System.out.println("Result of creating folder (excluding parent folder) :" + hasSucceeded);

   //mkdirs
   hasSucceeded = directory.mkdirs();
   System.out.println("Result of creating folder (including parent folder) :" + hasSucceeded);

}
Copy the code

The following output is displayed: The creation fails using mkdir, but succeeds using mkdirs.

Result of creating a folder (excluding the parent folder) :falseResult of creating folder (including parent folder) :true
Copy the code

As you can see, mkdir and mkdirs can create files, but they are very unfriendly to exception handling. If the creation fails, false is returned. The cause of the creation failure is not specified. Did the parent folder not exist? Or did the folder already exist so the creation failed? Failed to create folder because of disk I/O?

2. Java NIO create folder

In order to solve the problem of unclear exception failure handling in the traditional IO creation folder, improvements were made in Java NIO.

2.1.files. createDirectory Creates a folder

If the parent folder of the created folder does not exist, NoSuchFileException is thrown. If the created folder exists already, it is thrown FileAlreadyExistsException. IOException is thrown if an exception occurs due to disk I/O.

Path path = Paths.get("D:\\data222\\test");
Path pathCreate = Files.createDirectory(path);
Copy the code

2.2 Files. CreateDirectories creating folders and its parent folder

If the parent of the created folder does not exist, create it; If the created folder already exists, the folder will not be created repeatedly, and no exception will be thrown. IOException is thrown if an exception occurs due to disk I/O.

So, it’s possible to avoid using files.createDirectories where it’s possible to use files.createDirectory so that fewer exceptions are encountered.

Path path = Paths.get("D:\\data222\\test");
Path pathCreate = Files.createDirectorys(path);
Copy the code

Note that NIO API returns Path to create folders, so that we can continue to write file data into folders after creating them. Much better than traditional IO that returns only a Boolean value.

summary

In the Java language, there are four ways to create a specified directory, involving two different classes of java.io.File and java.nio.file.files

File.mkdir () returns true on success, false on failure. Also returns false if the parent folder of the created folder does not exist. No exceptions are thrown.

File.mkdirs () creates a folder with its parent folder, returning true on success and false on failure. Again, no exception is thrown for failed creation.

If the parent folder of the created folder does not exist, NoSuchFileException is thrown. If the created folder exists already, it is thrown FileAlreadyExistsException. IOException is thrown if an exception occurs due to disk I/O.

CreateDirectories Create a folder and its parent if the parent folder does not exist; If the created folder already exists, the folder will not be created repeatedly, and no exception will be thrown. IOException is thrown if an exception occurs due to disk I/O.

Of the four methods, the best method is to use files.createDirectories to create a folder.