Java - 如何清除文本文件而不删除它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29878237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:44:59  来源:igfitidea点击:

Java - How to Clear a text file without deleting it?

javafileclearformatter

提问by Kemosabe

I am wondering what the best way to clear a file is. I know that java automatically creates a file with

我想知道清除文件的最佳方法是什么。我知道java会自动创建一个文件

f = new Formatter("jibberish.txt");  
s = new Scanner("jibberish.txt");

if none already exists. But what if one exists and I want to clear it every time I run the program? That is what I am wondering: to say it again how do I clear a file that already exists to just be blank? Here is what I was thinking:

如果不存在。但是如果一个存在并且我想在每次运行程序时清除它怎么办?这就是我想知道的:再说一遍,我如何清除已存在的文件以使其成为空白?这是我的想法:

public void clearFile(){
    //go through and do this every time in order to delete previous crap
    while(s.hasNext()){
        f.format(" ");
    }
} 

采纳答案by Ankur Anand

If you want to clear the file without deleting may be you can workaround this

如果您想清除文件而不删除可能是您可以解决此问题

public static void clearTheFile() {
        FileWriter fwOb = new FileWriter("FileName", false); 
        PrintWriter pwOb = new PrintWriter(fwOb, false);
        pwOb.flush();
        pwOb.close();
        fwOb.close();
    }

Edit: It throws exception so need to catch the exceptions

编辑:它抛出异常所以需要捕捉异常

回答by CKing

You could delete the file and create it again instead of doing a lot of io.

您可以删除该文件并重新创建它,而不是执行大量 io。

if(file.delete()){
    file.createNewFile();
}else{
    //throw an exception indicating that the file could not be cleared
}

Alternately, you could just overwrite the contents of the file in one go as explained in the other answers :

或者,您可以一次覆盖文件的内容,如其他答案中所述:

PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();

Also, you are using the constructor from Scannerthat takes a Stringargument. This constructor will not read from a file but use the Stringargument as the text to be scanned. You should first created a file handle and then pass it to the Scannerconstructor :

此外,您正在使用Scanner带有String参数的构造函数。此构造函数不会从文件中读取,而是使用String参数作为要扫描的文本。您应该首先创建一个文件句柄,然后将其传递给Scanner构造函数:

File file = new File("jibberish.txt");
Scanner scanner = new Scanner(file);

回答by Sarath Kumar Sivan

You can just print an empty string into the file.

您可以将空字符串打印到文件中。

PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();

回答by TheCoder

Best I could think of is :

我能想到的最好的是:

Files.newBufferedWriter(pathObject , StandardOpenOption.TRUNCATE_EXISTING);

and

Files.newInputStream(pathObject , StandardOpenOption.TRUNCATE_EXISTING);

In both the cases if the file specified in pathObject is writable, then that file will be truncated. No need to call write() function. Above code is sufficient to empty/truncate a file.This is new in java 8.

在这两种情况下,如果 pathObject 中指定的文件是可写的,那么该文件将被截断。无需调用 write() 函数。上面的代码足以清空/截断文件。这是 Java 8 中的新功能。

Hope it Helps

希望能帮助到你