How to Create Directory In Java

Category: Java   Tags: Java, Java File Handling, Java Path, Java NIO File System

Java Classic IO Example

Below Java program will create a single and nested directories using classic Java IO classes:

JavaCreateDirectoryClassicIO.java

                                package com.tutorial.java.files;

                                import java.io.File;
                                import java.io.IOException;

                                public class JavaCreateDirectoryClassicIO {

                                    public static void main(String[] args)  throws IOException {
                                        File file = new File("/home/Desktop/demo");
                                        if (!file.exists()) {
                                            if (file.mkdir()) {
                                                System.out.println("Directory is created!");
                                            } else {
                                                System.out.println("Failed to create directory!");
                                            }
                                        }
                                        System.out.println("Create directory inside another directory");
                                        File nestedDir = new File("/home/Desktop/dir1/dir2/dir3");
                                        if (!nestedDir.exists()) {
                                            if (nestedDir.mkdirs()) {
                                                System.out.println("Directory is created!");
                                            } else {
                                                System.out.println("Failed to create directory!");
                                            }
                                        }
                                    }
                                }
                            

Java NIO Example

Below Java program will create a single and nested directories using Java NIO classes:

JavaCreateDirectoryNIO.java

                                package com.tutorial.java.files;

                                import java.io.IOException;
                                import java.nio.file.Files;
                                import java.nio.file.Path;
                                import java.nio.file.Paths;

                                public class JavaCreateDirectoryNIO {

                                    public static void main(String[] args)  throws IOException {
                                        Path path = Paths.get("/home/manish/Desktop/demo");
                                        if(!Files.exists(path)) {
                                            Files.createDirectory(path);
                                            System.out.println("Single directory created.");
                                        } else {
                                            System.out.println("Directory already exist.");
                                        }

                                        Path nestedPath = Paths.get("/home/manish/Desktop/dir1/dir2/dir3");
                                        if(!Files.exists(nestedPath)) {
                                            Files.createDirectories(nestedPath);
                                            System.out.println("Nested directory created.");
                                        } else {
                                            System.out.println("Directory already exist.");
                                        }
                                    }
                                }
                            

Create a Temporary Directory in Java

You can create a temporary directory using below methods of Files class:

                                public static Path move(Path source,
                                                        Path target,
                                                        CopyOption... options)
                                                 throws IOException

                                public static Path createTempFile(String prefix,
                                                                  String suffix,
                                                                  FileAttribute<?>... attrs)
                                                           throws IOException