java FileOutputStream:“关闭”方法是否也调用“刷新”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31456660/
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
FileOutputStream: Does the "close" method calls also "flush"?
提问by javauser35
I'm really confused about flush and close method.In my code I always close my FileOutputStream
object. But I want to know that if I have to use flush method here, and where can I use it?
我真的对刷新和关闭方法感到困惑。在我的代码中,我总是关闭我的FileOutputStream
对象。但是我想知道如果我这里必须使用flush方法,我在哪里可以使用它?
I will write a project that download 4 or 5 files repeatedly. I will write a method(for download files) and my method will be in a loop and download files repeatedly.My method will have a code like this.
我将编写一个重复下载 4 或 5 个文件的项目。我将编写一个方法(用于下载文件),我的方法将循环并重复下载文件。我的方法将有这样的代码。
Does the close
method calls flush
, or do I have to use flush before closing?
该close
方法是否调用flush
,或者我是否必须在关闭之前使用刷新?
try {
InputStream inputStream = con.getInputStream();
FileOutputStream outputStream = new FileOutputStream("C:\programs\TRYFILE.csv");
int bytesRead = -1;
byte[] buffer = new byte[4096];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} catch(Exception e) {
//
} finally {
outputStream.close();
inputStream.close();
}
Note that the code works well: it download the file successfully. But I'm not sure about using flush
.
请注意,代码运行良好:它成功下载了文件。但我不确定使用flush
.
回答by Davide Lorenzo MARINO
The method flush
is used to "flush" bytes retained in a buffer. FileOutputStream
doesn't use any buffer, so flush method is empty. Calling it or not doesn't change the result of your code.
该方法flush
用于“刷新”保留在缓冲区中的字节。FileOutputStream
不使用任何缓冲区,因此刷新方法为空。调用与否不会改变代码的结果。
With buffered writers the method close
call explicitly flush
.
使用缓冲写入器close
显式调用方法flush
。
So you need to call flush when you like to write the data before closing the streamand before the buffer is full (when the buffer is full the writer starts writing without waiting a flush call).
因此,当您想在关闭流之前和缓冲区已满之前写入数据时,您需要调用flush (当缓冲区已满时,编写器无需等待刷新调用即可开始写入)。
The source code of class FileOutputStream
hasn't a custom version of method flush
. So the flush
method used is the version of its super class OutputStream
. The code of flush in OutputStream
is the following
class 的源代码FileOutputStream
没有自定义版本的 method flush
。所以使用的flush
方法是它的超类的版本OutputStream
。flush in的代码OutputStream
如下
public void flush() throws IOException {
}
As you see this is an empty method doing nothing, so calling it or not is the same.
正如你所看到的,这是一个什么都不做的空方法,所以调用它与否是一样的。
回答by Puce
I will write a project that download 4 or 5 files repeatedly. I will write a method(for download files) and my method will be in a loop and download files repeatedly.My method will have a code like this.
Does the close method calls flush, or do I have to use flush before closing?
我将编写一个重复下载 4 或 5 个文件的项目。我将编写一个方法(用于下载文件),我的方法将循环并重复下载文件。我的方法将有这样的代码。
close 方法是调用flush,还是必须在关闭前使用flush?
I recommend to use the NIO.2 API and the try-with-resources statement. This will reduce the amount of code and takes care of flushing and closing the streams:
我推荐使用 NIO.2 API 和 try-with-resources 语句。这将减少代码量并负责刷新和关闭流:
try (InputStream inputStream = con.getInputStream()){
Files.copy(inputStream, Paths.get("C:\programs\TRYFILE.csv"));
}
The topic is a bit confusing since OutputStream.closedoes indeed not require an automatic flush, but subclasses might specify that. They might also provide a flush method which does nothing (e.g. as the one inherited from OutputStream, which is the case for FileOutputStream). In this case it has no effect to call the flush method, of course, so you can omit it.
这个主题有点令人困惑,因为OutputStream.close确实不需要自动刷新,但子类可能会指定。他们还可能提供一个什么都不做的刷新方法(例如,作为从 OutputStream 继承的方法,FileOutputStream 就是这种情况)。这种情况下调用flush方法是没有效果的,当然可以省略。
If in doubt (if you don't know which subclass you're working with) I guess it's better to call the flush manually.
如果有疑问(如果您不知道您正在使用哪个子类),我想最好手动调用刷新。
But again, using the code above this is taken care for you.
但同样,使用上面的代码会照顾到您。