1String path = ...
2File pathAsFile = new File(path);
3
4if (!Files.exists(Paths.get(path))) {
5 pathAsFile.mkdir();
6}
1package com.mkyong.file;
2
3import java.io.File;
4
5public class CreateDirectoryExample {
6
7 public static void main(String[] args) {
8
9 File file = new File("C:\\Directory1");
10 if (!file.exists()) {
11 if (file.mkdir()) {
12 System.out.println("Directory is created!");
13 } else {
14 System.out.println("Failed to create directory!");
15 }
16 }
17
18 File files = new File("C:\\Directory2\\Sub2\\Sub-Sub2");
19 if (!files.exists()) {
20 if (files.mkdirs()) {
21 System.out.println("Multiple directories are created!");
22 } else {
23 System.out.println("Failed to create multiple directories!");
24 }
25 }
26
27 }
28
29}
1// mkdir() method in java example
2import java.io.*;
3public class CreateDirectory
4{
5 public static void main(String[] args)
6 {
7 File directory = new File("D:\\DirectoryExample");
8 if(!directory.exists())
9 {
10 if(directory.mkdir())
11 {
12 System.out.println("Directory is created");
13 }
14 else
15 {
16 System.out.println("Directory not created");
17 }
18 }
19 }
20}
1// mkdirs() method in java example
2import java.io.File;
3public class CreateDirectoryDemo
4{
5 public static void main(String[] args)
6 {
7 File directory = new File("D:\\DirectoryExample\\subdirectory1\\subdirectory2");
8 if(!directory.exists())
9 {
10 if(directory.mkdirs())
11 {
12 System.out.println("Sub directories created");
13 }
14 else
15 {
16 System.out.println("Sub directories not created");
17 }
18 }
19 }
20}
1 Path path = Paths.get("/home/mkyong/a/b/c/");
2
3 //java.nio.file.Files;
4 Files.createDirectories(path);