FileWriter fwriter = new FileWriter("C:/Users/NULL NULL NULL/Desktop/New Text Document.txt", false); // set the write to false so we dont append to it
BufferedWriter write = new BufferedWriter(fwriter); // this is the type so we can write to it.
write.write(""); // write empty string to delet everything
write.close(); // close the buffer to gain back memory space that the buffer
占用。
这个怎么样:
File file = new File("myfile");
if(file.exists()){
file.delete();
}
file.createNewFile();
或者,这将创建一个新文件或覆盖现有文件:
Files.newBufferedWriter(Paths.get("myfile"));
或者@CandidOrange的印刷作者:
new PrintWriter("myfile").close();
否则,好的老办法:
File file = new File("myfile");
FileOutputStream fooStream = new FileOutputStream(file, false);
fooStream.write("".getBytes());
fooStream.close();