- Given a path in java.
- Create a new folder or directory using File class.
- Create a file using File class
- File class has following methods to create file & directory in java.
No. | File Api | Description |
1 | boolean mkdir() | Creates the directory named by this abstract pathname. |
2 | boolean mkdirs() | Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. |
3 | boolean createNewFile() | Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. |
- We will create directory in non existed path using mkdir api.
- Also, we will create new directory & file using mkdir & createNewFile respectively.
Program: create new file & folder (directory) – java
package org.learn.io;
import java.io.File;
import java.io.IOException;
public class CreateFileDirectoryJava {
public static void main(String[] args) throws IOException {
String dirPath = "C:\\nonExistedDirectory\\directory";
String newDirName = "dir1";
String newFileName = "file1.txt";
String anotherNewFileName = "file2.txt";
File newDirectory = new File(dirPath);
//Create directory for non existed path.
boolean isCreated = newDirectory.mkdirs();
if (isCreated) {
System.out.printf("1. Successfully created directories, path:%s",
newDirectory.getCanonicalPath());
} else if (newDirectory.exists()) {
System.out.printf("1. Directory path already exist, path:%s",
newDirectory.getCanonicalPath());
} else {
System.out.println("1. Unable to create directory");
return;
}
//Create file under new directory path C:\newDirectory\directory
File newFile = new File(dirPath + File.separator + newFileName);
//Create new file under specified directory
isCreated = newFile.createNewFile();
if (isCreated) {
System.out.printf("\n2. Successfully created new file, path:%s",
newFile.getCanonicalPath());
} else { //File may already exist
System.out.printf("\n2. Unable to create new file");
}
//Create new directory under C:\nonExistedDirectory\directory
File oneMoreDirectory = new File(dirPath + File.separator + newDirName);
//Create directory for existed path.
isCreated = oneMoreDirectory.mkdir();
if (isCreated) {
System.out.printf("\n3. Successfully created new directory, path:%s",
oneMoreDirectory.getCanonicalPath());
} else { //Directory may already exist
System.out.printf("\n3. Unable to create directory");
}
//Create file under new directory path C:\newDirectory\directory
File anotherNewFile = new File(oneMoreDirectory + File.separator + anotherNewFileName);
//Create new file under specified directory
isCreated = anotherNewFile.createNewFile();
if (isCreated) {
System.out.printf("\n4. Successfully created new file, path:%s",
anotherNewFile.getCanonicalPath());
} else { //File may already exist
System.out.printf("\n4. Unable to create new file");
}
}
}
Output: create new file & directory (folder) in java
1. Successfully created directories, path:C:\nonExistedDirectory\directory
2. Successfully created new file, path:C:\nonExistedDirectory\directory\file1.txt
3. Successfully created new directory, path:C:\nonExistedDirectory\directory\dir1
4. Successfully created new file, path:C:\nonExistedDirectory\directory\dir1\file2.txt