java 如何清空文件内容,然后多次追加文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11233866/
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-10-31 04:20:01  来源:igfitidea点击:

How to empty file content and then append text multiple times

javafile-io

提问by HBv6

I have a file (file.txt), and I need to empty his current content, and then to append some text multiple times.

我有一个文件(file.txt),我需要清空他当前的内容,然后多次附加一些文本。

Example: file.txt current content is:

示例:file.txt 当前内容为:

aaa

bbb

ccc

啊啊啊

bbb

抄送

I want to remove this content, and then to append the first time:

我想删除这个内容,然后第一次追加:

ddd

滴滴

The second time:

第二次:

eee

ee

And so on...

等等...

I tried this:

我试过这个:

// empty the current content
fileOut = new FileWriter("file.txt");
fileOut.write("");
fileOut.close();

// append
fileOut = new FileWriter("file.txt", true);

// when I want to write something I just do this multiple times:
fileOut.write("text");
fileOut.flush();

This works fine, but it seems inefficient because I open the file 2 times just for remove the current content.

这工作正常,但似乎效率低下,因为我打开文件 2 次只是为了删除当前内容。

回答by Rob Wagner

When you open up the file to write it with your new text, it will overwrite whatever is in the file already.

当您打开文件以使用新文本写入时,它将覆盖文件中已有的任何内容。

A good way to do this is

这样做的一个好方法是

// empty the current content
fileOut = new FileWriter("file.txt");
fileOut.write("");
fileOut.append("all your text");
fileOut.close();

回答by Conor Sherman

The first answer is not correct. If you create a new filewriter with the true flag for the second parameter, it will open in append mode. This will cause any write(string) commands to "append" text to the end of the file, not wipe out whatever text is already there.

第一个答案不正确。如果您使用第二个参数的 true 标志创建一个新的文件写入器,它将以追加模式打开。这将导致任何 write(string) 命令将文本“附加”到文件的末尾,而不是清除已经存在的任何文本。

回答by HBv6

I'm just stupid.

我只是愚蠢。

I only needed to do this:

我只需要这样做:

// empty the current content
fileOut = new FileWriter("file.txt");

// when I want to write something I just do this multiple times:
fileOut.write("text");
fileOut.flush();

And AT THE END close the stream.

最后关闭流。

回答by alisa

I see that this question was answered quite a few Java versions ago... Starting from Java 1.7 and using the new FileWriter + BufferWriter + PrintWriter for appending (as recommended in this SO answer), my suggestion for file erasing and then appending:

我看到这个问题在很多 Java 版本之前得到了回答......从 Java 1.7 开始并使用新的 FileWriter + BufferWriter + PrintWriter 进行附加(如本 SO answer 中所推荐的),我对文件擦除然后附加的建议:

FileWriter fw = new FileWriter(myFilePath); //this erases previous content
fw = new FileWriter(myFilePath, true); //this reopens file for appending 
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println("text"); 
//some code ...
pw.println("more text"); //appends more text 
pw.flush();
pw.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

希望能帮助到你