简体   繁体   English

对文件使用PrintWriter

[英]Using PrintWriter with files

In case I have the following code: 如果我有以下代码:

private PrintWriter m_Writer;
m_Writer = new PrintWriter(new FileWriter(k_LoginHistoryFile));

I am writing to a local file on server which name is k_LoginHistoryFile . 我正在写入名称为k_LoginHistoryFile服务器上的本地文件。
Now, as my program runs it doing writings to this file so how can I delete all the file content between each writes? 现在,在我的程序运行时,它正在对此文件进行写操作,那么如何删除每次写操作之间的所有文件内容?
I think it is important as I don't want to write to a file which will eventually have current updated information on its beginning + not up to date info at its end. 我认为这很重要,因为我不想写入一个文件,该文件最终将在开始时具有当前更新的信息+在其末尾没有最新信息。

Thanks in advance 提前致谢

This expression: 该表达式:

new FileWriter(k_LoginHistoryFile)

will truncate your file if it exists. 将截断您的文件(如果存在)。 It won't just overwrite the start of the file. 它不仅会覆盖文件的开始。 It's not clear how often this code is executing, but each time it does execute, you'll start a new file (and effectively delete the old contents). 目前尚不清楚这些代码是如何经常执行,但每次执行的时候,你就会开始一个新的文件(并有效地删除旧的内容)。

I think it is important as I don't want to write to a file which will eventually have current updated information on its beginning + not up to date info at its end. 我认为这很重要,因为我不想写入一个文件,该文件最终将在开始时具有当前更新的信息+在其末尾没有最新信息。

If you want to keep a running output file (and you can't keep the file open), consider this constructor: FileWriter(String, boolean) 如果要保持运行中的输出文件(并且无法保持打开状态),请考虑以下构造函数: FileWriter(String,boolean)

If the boolean is true , your updated information will be at the end of the file. 如果布尔值为true ,则更新的信息在文件末尾。

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

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