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?
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?
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()
, but you must call `close() – Bumper