Create a file in Java
The File class has three constructors and a number of useful methods. The following are the three constructors:
File("file name")
File("Directory name")
File("directory name", "Filename")

import java.util.*;
import java.io.*;
class TestClass{
public static void main( String[] args )
{
try{
File file = new File("c:\\temp.txt");
if (file.createNewFile()){ //return true
System.out.println("New file is created!!");
}else{ //return false
System.out.println("temp.txt already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
New file is created!
Related Topics