[Java] How to create a folder

Programming study diary

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.

How to create a folder

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.

Sample code

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();
}

How to get the creation date and time of a folder

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

References

[Introduction to Java] How to create a folder (java.nio.file) Create Directory

Recommended Posts

[Java] How to create a folder
How to create a method
How to create a Java environment in just 3 seconds
How to make a Java container
How to create a data URI (base64) in Java
How to make a Java array
How to create a lightweight container image for Java apps
How to make a Java calendar Summary
[Introduction to Java] How to write a Java program
How to create a Maven repository for 2020
How to make a Discord bot (Java)
How to print a Java Word document
[Swift5] How to create a splash screen
[rails] How to create a partial template
[Java] Create a filter
How to create docker-compose
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to create a database for H2 Database anywhere
[Rails] How to create a graph using lazy_high_charts
How to display a web page in Java
Try to create a bulletin board in Java
How to create pagination for a "kaminari" array
How to create a class that inherits class information
How to convert a solidity contract to a Java contract class
How to create a theme in Liferay 7 / DXP
[1st] How to create a Spring-MVC framework project
How to easily create a pull-down in Rails
[Rails] How to create a Twitter share button
[Java] How to get the authority of the folder
How to create an application
[Java] How to use Map
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
Java --How to make JTable
How to use java Optional
How to minimize Java images
How to write java comments
How to leave a comment
Create a java method [Memo] [java11]
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] Create a temporary file
[Java] How to use string.format
How to use Java Map
How to set Java constants
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
How to initialize Java array
How to insert a video
[Java] How to play rock-paper-scissors (equivalent to paiza rank A)
How to create a JDBC URL (Oracle Database, Thin)
How to create a Spring Boot project in IntelliJ
I tried to create a Clova skill in Java
[Java] How to get a request by HTTP communication
[Java] How to execute tasks on a regular basis
[Java] How to cut out a character string character by character