EN
Java - create directory with subdirectory
0 points
In this article, we would like to show you how to create a directory with a subdirectory in Java.
xxxxxxxxxx
1
Directory/
2
|
3
+-- subirectory1/
4
|
5
+-- subdirectory2/
Practical example
In this example, we will create the following tree using mkdirs()
method:
xxxxxxxxxx
1
Directory/
2
|
3
+-- subirectory1/
4
|
5
+-- subdirectory2/
code example:
xxxxxxxxxx
1
import java.io.File;
2
3
public class Example {
4
5
public static void main(String[] args) {
6
File myDirectory = new File("C:\\projects\\example\\Directory\\subdirectory1\\subdirectory2");
7
8
if (!myDirectory.exists()) {
9
if (myDirectory.mkdirs()) {
10
System.out.println("New directory created succesfully.");
11
} else {
12
System.out.println("Couldn't create directory.");
13
}
14
} else {
15
System.out.println("Directory already exists.");
16
}
17
}
18
}
