Saturday, September 1, 2018

How to create directory in Java

To create a directory in Java, just use the mkdir or mkdirs methods of the Java File class. you can see in below example, both methods (mkdir and mkdirs) return a boolean value indicating whether the directories were created successfully. You'll want to include your own logic in the if/then tests that are shown after the mkdir and mkdirs method calls.

How to create directory in Java

To create a directory in Java, uses the following code:

Method-1 Using Standard Java IO package – java.io.File
  • If you require to create single folder or directory in java application, refer the below java code :
new File("C:\\file1").mkdir();
  • if you require to create multiple folder in nested order. For example here we are creating file2 folder inside that we are creating sub directory Sub1 and Sub2.
new File("C:\\file2\\Sub1\\Sub2").mkdirs()

Method-2 : For JDK 7, try Java NIO package – java.nio.file.Paths and java.nio.file.Files
Path path = Paths.get("C:\\File1");
Files.createDirectories(path);

Let see the complete examples :

Java IO Example

A classic Java IO directory example, check if directory exists, if no, then create it.
import java.io.File;

public class CreateDirectoryExample {

    public static void main(String[] args) {

        File file = new File("C:\\File1");
        if (!file.exists()) {
            if (file.mkdir()) {
                System.out.println("Directory is created!");
            } else {
                System.out.println("Failed to create directory!");
            }
        }

        File files = new File("C:\\File2\\Sub2\\Sub-Sub2");
        if (!files.exists()) {
            if (files.mkdirs()) {
                System.out.println("Multiple directories are created!");
            } else {
                System.out.println("Failed to create multiple directories!");
            }
        }

    }

}

Java NIO Example

Java NIO classes are added in JDK 7.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateDirectoryExample {
    public static void main(String[] args) {

        Path path = Paths.get("C:\\Directory2\\Sub2\\Sub-Sub2");
        //if directory exists?
        if (!Files.exists(path)) {
            try {
                Files.createDirectories(path);
            } catch (IOException e) {
                //fail to create directory
                e.printStackTrace();
            }
        }

    }
    
}

If a directory is failing to create, IOException will be thrown, for example
java.nio.file.AccessDeniedException: /directory-name
 at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
 at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
 at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
 at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:384)
 at java.nio.file.Files.createDirectory(Files.java:674)
 at java.nio.file.Files.createAndCheckIsDirectory(Files.java:781)
 at java.nio.file.Files.createDirectories(Files.java:767)

Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.



No comments:

Post a Comment