We create a directory by calling mkdir()
method of the File
object. This method returns true
if and only if the directory was successfully created. If false
was returned, no directory is created. This could be caused, for example that the directory was already exists.
package org.kodejava.io;
import java.io.File;
public class CreateDirectoryExample {
public static void main(String[] args) {
File file = new File("tmpdir");
if (file.mkdir()) {
System.out.println("Directory = " + file.getAbsolutePath());
} else {
System.out.println("No directory was created");
}
}
}
Here is the result of the program:
Directory = F:\Wayan\Kodejava\kodejava-example\tmpdir
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024