January 3, 2021
A brief summary of how to create a folder. This is a summary of folder creation with java.nio (NEW I/O 2)
, which has improved functions from Java 7.
Use get method of java.niofile.Paths class to specify the path of the folder to be created. If you want to create only one folder, use the createDirectory method of the java.nio.Files class. Use the createDirectories method to create all the folder hierarchies including the parent folder at once.
If the file already exists, you will get a FileAlreadyExistsException exception.
try {
//Create with absolute path
Path path1 = Paths.get("c:\\Test1");
Files.createDirectory(path1);
//Created including the parent folder
Path pat<h2><span id="_PathsgetquotcTest2abcquot">= Paths.get("c:\\Test1\\abc");</span></h2>
Files.createDirectories(path2);
//Create with relative path
Path pat<h3><span id="_PathsgetquotTest1quot">= Paths.get("Test1");</span></h3>
Files.createDirectory(path3);
} catch (IOException e) {
e.printStackTrace();
}
In java.nio, you can get the creation date and time of the folder. Get by setting creatorTime as an argument with getAttribute method.
Path path = Paths.get("c:\\Test1");
System.out.println(Files.getAttribute(path, "creationTime"));
Execution result
2020-01-03T02:54:45.903849Z
[Introduction to Java] How to create a folder (java.nio.file) Create Directory
Recommended Posts