简体   繁体   English

FileOutputStream:“close”方法是否也调用“flush”?

[英]FileOutputStream: Does the “close” method calls also “flush”?

I'm really confused about flush and close method.In my code I always close my FileOutputStream object. 我真的很困惑flush和close方法。在我的代码中,我总是关闭我的FileOutputStream对象。 But I want to know that if I have to use flush method here, and where can I use it? 但我想知道如果我必须在这里使用flush方法,我可以在哪里使用它?

I will write a project that download 4 or 5 files repeatedly. 我将编写一个重复下载4或5个文件的项目。 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? close方法是否调用flush ,或者在关闭之前是否必须使用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

The method flush is used to "flush" bytes retained in a buffer. 方法flush用于“刷新”保留在缓冲区中的字节。 FileOutputStream doesn't use any buffer, so flush method is empty. FileOutputStream不使用任何缓冲区,因此flush方法为空。 Calling it or not doesn't change the result of your code. 调用与否不会改变代码的结果。

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 stream and 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 . FileOutputStream类的源代码没有方法flush的自定义版本。 So the flush method used is the version of its super class OutputStream . 因此使用的flush方法是其超类OutputStream的版本。 The code of flush in OutputStream is the following OutputStream中的flush代码如下

public void flush() throws IOException {
}

As you see this is an empty method doing nothing, so calling it or not is the same. 如你所见,这是一个空的方法什么都不做,所以调用与否是相同的。

I will write a project that download 4 or 5 files repeatedly. 我将编写一个重复下载4或5个文件的项目。 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? close方法是否调用flush,或者在关闭之前是否必须使用flush?

I recommend to use the NIO.2 API and the try-with-resources statement. 我建议使用NIO.2 API和try-with-resources语句。 This will reduce the amount of code and takes care of flushing and closing the streams: 这将减少代码量并负责刷新和关闭流:

try (InputStream inputStream = con.getInputStream()){
    Files.copy(inputStream, Paths.get("C:\\programs\\TRYFILE.csv"));
}

The topic is a bit confusing since OutputStream.close does indeed not require an automatic flush, but subclasses might specify that. 该主题有点令人困惑,因为OutputStream.close确实不需要自动刷新,但是子类可能指定了这一点。 They might also provide a flush method which does nothing (eg as the one inherited from OutputStream, which is the case for FileOutputStream). 它们也可能提供一个不执行任何操作的flush方法(例如,从OutputStream继承的方法,这是FileOutputStream的情况)。 In this case it has no effect to call the flush method, of course, so you can omit it. 在这种情况下,当然,调用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. 如果有疑问(如果你不知道你正在使用哪个子类),我想最好手动调用flush。

But again, using the code above this is taken care for you. 但同样,使用上面的代码会照顾你。

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

相关问题 确实关闭套接字也会关闭/刷新输入/输出流 - does close a socket will also close/flush the input/output stream 语句中的 Close 方法是否也在 JDBC 中中止 DB 的请求 - Does Close method in Statement abort the request of DB also in JDBC 刷新并关闭后,BufferedWriter不会写入文件 - BufferedWriter does not write to file after flush and close Outputstream的Flush方法不起作用 - The Flush method of outputstream does nothing OutputStream的flush方法什么都不做? - The flush method of OutputStream does nothing? 如何解决这个问题 关闭这个“FileOutputStream” - How to solve this Close this "FileOutputStream" SocketChannel.close()是否也会关闭套接字? - Does SocketChannel.close() close the socket also? 在AsyncTask的doInBackground()方法中运行FileOutputStream操作无法成功执行 - Running FileOutputStream operations in AsyncTask's doInBackground() method does not execute successfully FileOutputStream在空间不足时关闭 - FileOutputStream close when out of space 如果我们在Java中使用flush()但不使用close()方法,是否将垃圾回收PrintWriter对象? - Will the PrintWriter object will be garbage collected if we use flush() but not close() method in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM