Java New File Creation Example
Chapter:
File
Last Updated:
03-09-2016 12:26:02 UTC
Program:
/* ............... START ............... */
import java.io.File;
import java.io.IOException;
public class JavaNewFileCreation {
public static void main(String[] args) {
try {
File file = new File("C:/myfile.txt");
if (file.createNewFile())
System.out.println("Success!");
else
System.out.println("Error, file already exists.");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
/* ............... END ............... */
Output
Notes:
-
Java creates a new file by using File() constructor and file.createNewFile() method of File class.
Tags
New File Creation, Java, File