如何在不删除java中的文件的情况下清空文件内容


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();

相关内容

  • 没有找到相关文章

最新更新