The following example demonstrates using the File() constructor and file.createNewFile() method of the File class to create a new file
/* author by w3cschool.cc Main.java */import java.io.File;import java.io.IOException;public class Main { public static void main(String[] args) { try{ File file = new File("C:/myfile.txt"); if(file.createNewFile()) System.out.println("文件创建成功!"); else System.out.println ("出错了,改文件已经存在。"); } catch(IOException ioe) { ioe.printStackTrace(); } } }
The output result of the above code is:
文件创建成功!
The above is the Java example - the content of the created file. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!