When creating a file with java, use the createNewFile method of the File class. File newfile = new File ("/ Users / Shared / java / filename"); If a file with the same name already exists, it will fail to be created and will return false.
Sample code
public class fileClass{
public fileClass() throws IOException{
File file = new File("/Users/Shared/java/java.txt");
//Create a file using the createNewFile method
if (file.createNewFile()){
System.out.println("File creation successful");
}else{
System.out.println("File creation failure");
}
}
}
Note that if the specified destination directory cannot be found, an "IOException" exception will occur.
Recommended Posts