Home »
Java Programs »
Java File Handling Programs
Java program to create a new file
How to Create New File in Java?
File class is used for file related operations, in this program we will use createNewFile() method of File class that will return boolean value, if file is created successfully, method will retutn true otherwise it will return false.
Create New File using Java Program
// Java program to create new file
import java.io.File;
public class CreateFile {
public static void main(String args[]) {
final String fileName = "file1.txt";
try {
File objFile = new File(fileName);
if (objFile.createNewFile()) {
System.out.println("File created successfully.");
} else {
System.out.println("File creation failed!!!");
}
} catch (Exception Ex) {
System.out.println("Exception : " + Ex.toString());
}
}
}
Output
File created successfully.
Java File Handling Programs »