简体   繁体   English

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

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

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

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.在这两种情况下,如果 pathObject 中指定的文件是可写的,那么该文件将被截断。 No need to call write() function.无需调用 write() 函数。 Above code is sufficient to empty/truncate a file.This is new in java 8.上面的代码足以清空/截断文件。这是 Java 8 中的新功能。

Hope it Helps希望能帮助到你

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编辑:它抛出异常所以需要捕捉异常

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 Scanner that takes a String argument.此外,您正在使用来自Scanner的构造函数,该构造函数采用String参数。 This constructor will not read from a file but use the String argument as the text to be scanned.此构造函数不会从文件中读取,而是使用String参数作为要扫描的文本。 You should first created a file handle and then pass it to the Scanner constructor :您应该首先创建一个文件句柄,然后将其传递给Scanner构造函数:

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

You can just print an empty string into the file.您可以将空字符串打印到文件中。

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

类型

new PrintWriter(PATH_FILE).close();

Better to use this:最好使用这个:

    public static void clear(String filename) throws IOException {
    FileWriter fwOb = new FileWriter(filename, false); 
    PrintWriter pwOb = new PrintWriter(fwOb, false);
    pwOb.flush();
    pwOb.close();
    fwOb.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM