Java 何时刷新 BufferedWriter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/908168/
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
When to flush a BufferedWriter
提问by LB40
In a Java program (Java 1.5), I have a BufferedWriter that wraps a Filewriter, and I call write() many many times... The resulting file is pretty big...
在 Java 程序(Java 1.5)中,我有一个包装 Filewriter 的 BufferedWriter,我多次调用 write() ......生成的文件非常大......
Among the lines of this file, some of them are incomplete...
在这个文件的几行中,有些是不完整的......
Do I need to call flush each time I write something (but I suspect it would be inefficient) or use another method of BufferedWriter or use another class...?
每次我写东西时都需要调用flush吗(但我怀疑它效率低下)或使用BufferedWriter的另一种方法或使用另一个类......?
(Since I've a zillion lines to write, I do want to have something quite efficient.) What would be the ideal "flushing" moment? (when I reach the capacity of the BufferedWriter)...
(因为我有无数行要写,所以我确实想要一些非常有效的东西。)什么是理想的“冲洗”时刻?(当我达到 BufferedWriter 的容量时)...
Init:
在里面:
try {
analysisOutput = new BufferedWriter(new FileWriter(
"analysisResults", true));
analysisOutput.newLine();
analysisOutput.write("Processing File " + fileName + "\n");
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
Writing:
写作:
private void printAfterInfo(String toBeMoved,HashMap<String, Boolean> afterMap, Location location)
throws IOException {
if(afterMap != null) {
for (Map.Entry<String, Boolean> map : afterMap.entrySet()) {
if (toBeMoved == "Condition") {
if (1 <= DEBUG)
System.out.println("###" + toBeMoved + " " + location + " "
+ conditionalDefs.get(conditionalDefs.size() - 1)
+ " After " + map.getKey() + " "
+ map.getValue() + "\n");
analysisOutput.write("###" + toBeMoved + " " + location + " "
+ conditionalDefs.get(conditionalDefs.size() - 1)
+ " After " + map.getKey() + " " + map.getValue()
+ "\n");
} else {
if (1 <= DEBUG)
System.out.println("###" + toBeMoved + " " + location + " "
+ map.getKey() + " After "
+ map.getValue() + "\n");
if (conditionalDefs.size() > 0)
analysisOutput.write("###" + toBeMoved + " " + location + " "
+ conditionalDefs.get(conditionalDefs.size() - 1) + " "
+ map.getKey() + " After " + map.getValue()
+ "\n");
else
analysisOutput.write("###" + toBeMoved + " " + location + " " + map.getKey() + " After " + map.getValue() + "\n");
}
}
}
I've just figured out that the lines which are incomplete are those just before "Processing file"... so it occurs when I'm switching from one file that I analyze to another...
我刚刚发现不完整的行是在“处理文件”之前的那些行......所以当我从一个我分析的文件切换到另一个文件时会发生这种情况......
Closing:
闭幕:
dispatch(unit);
try {
if (analysisOutput != null) {
printFileInfo();
analysisOutput.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
Sometimes the information printed out by printFileInfo does not appear in the results file...
有时printFileInfo 打印出来的信息并没有出现在结果文件中...
采纳答案by Jon Skeet
The BufferedWriter
will already flush when it fills its buffer. From the docs of BufferedWriter.write
:
在BufferedWriter
当它填充它的缓存就会刷新已。来自以下文档BufferedWriter.write
:
Ordinarily this method stores characters from the given array into this stream's buffer, flushing the buffer to the underlying stream as needed.
通常,此方法将给定数组中的字符存储到此流的缓冲区中, 并根据需要将缓冲区刷新到底层流。
(Emphasis mine.)
(强调我的。)
The point of BufferedWriter
is basically to consolidate lots of little writes into far fewer big writes, as that's usually more efficient (but more of a pain to code for). You shouldn't need to do anything special to get it to work properly though, other than making sure you flush it when you're finishedwith it - and calling close()
will do this and flush/close the underlying writer anyway.
重点BufferedWriter
基本上是将大量的小写合并为更少的大写,因为这通常更有效(但编码更麻烦)。不过,除了确保在完成后刷新它之外,您不需要做任何特殊的事情来使其正常工作- 并且调用close()
将执行此操作并刷新/关闭底层编写器。
In other words, relax - just write, write, write and close :) The only time you normally need to call flush
manually is if you really, really need the data to be on disk now. (For instance, if you have a perpetual logger, you might want to flush it every so often so that whoever's reading the logs doesn't need to wait until the buffer's full before they can see new log entries!)
换句话说,放松 - 只需写入,写入,写入和关闭 :) 您通常需要flush
手动调用的唯一时间是您现在真的,真的需要将数据存储在磁盘上。(例如,如果您有一个永久记录器,您可能希望经常刷新它,以便读取日志的人无需等到缓冲区已满才能看到新的日志条目!)
回答by Dave
The ideal flushing moment is when you need another program reading the file to see the data that's been written, before the file is closed. In many cases, that's never.
理想的刷新时刻是在文件关闭之前,您需要另一个程序读取文件以查看已写入的数据。在许多情况下,这从来都不是。
回答by laurent
If you have a loop alternating init
and printAfterInfo
, my guess about your problem is that you don't close
your writer before creating a new one on the same file. You'd better create the BufferedWriter
once and close it at the end of all the processing.
如果你有一个循环交替init
和printAfterInfo
,我对你的问题的猜测是你close
在同一个文件上创建一个新的之前不是你的作者。您最好创建BufferedWriter
一次并在所有处理结束时关闭它。