Is it a bad thing to do multiple file closings in single finally block?
Relevant excerpt:
BufferedInputStream inStream;
BufferedOutputStream destStream;
try {
inStream = new BufferedInputStream(/*...*/);
destStream = new BufferedOutputStream(/*...*/);
// ... reading from file
// ... processing
// ... writting to file
destStream.flush();
} catch (Exception e) {
Log("ERROR: " + e.getMessage());
} finally {
try { destStream.close(); } catch (Exception ignored) {}
try { inStream.close(); } catch (Exception ignored) {}
}
EDIT-1: I cant have java7 (so I can't use try-with
from java7) :(
-
\$\begingroup\$ Java 6 is no longer being updated publicly. I don't suppose you enjoy paying for Oracle Premium Support for Java 6? Or are you using IBM Java 6? \$\endgroup\$200_success– 200_success2017年03月09日 08:52:06 +00:00Commented Mar 9, 2017 at 8:52
-
\$\begingroup\$ "Enjoy" is a word not related to my work. (changing tools used in organization is hard task, unless you own "gartner.com" email) \$\endgroup\$industryworker3595112– industryworker35951122017年03月09日 13:04:20 +00:00Commented Mar 9, 2017 at 13:04
3 Answers 3
As Nolequen wrote, his option is better to use. But it's only possible in Java 7+
If you want to proceed with your solution you can create one try and put close inside.
try {
BufferedInputStream inStream = new BufferedInputStream(/*...*/);
BufferedOutputStream destStream = new BufferedOutputStream(/*...*/);
// ... reading from file
// ... processing
// ... writting to file
destStream.flush();
} catch (Exception e) {
Log("ERROR: " + e.getMessage());
} finally {
try {
destStream.close();
inStream.close();
} catch (Exception ignored) {}
}
It is better to use try-with-resource statement. So you will not need to close streams manualy:
try (final BufferedInputStream inStream = new BufferedInputStream(/*...*/);
final BufferedOutputStream destStream = new BufferedOutputStream(/*...*/)) {
//do some work
} catch (Exception e) {
Log("ERROR: " + e.getMessage());
}
-
1\$\begingroup\$ That definitely seems better in many ways, but it is java7 only. Right? \$\endgroup\$industryworker3595112– industryworker35951122017年03月09日 07:56:20 +00:00Commented Mar 9, 2017 at 7:56
-
\$\begingroup\$ Yes, you right. Prior to Java 7, you can use a finally block with multiple close(). \$\endgroup\$Nolequen– Nolequen2017年03月09日 15:26:31 +00:00Commented Mar 9, 2017 at 15:26
Both, InputStream
and OutputStream
implement the Closable
interface in Java6 so you can write your own exception ingnoring closer object/method:
private void closeIgnoringException(Closable closable){
try { closable.close(); }
catch (Exception ignored) {
ignored.printStacktrace();
}
}
Set<Closable> closables = new HasSet<Closable>();
try {
BufferedInputStream inStream = new BufferedInputStream(/*...*/);
closables.add(inStream);
BufferedOutputStream destStream = new BufferedOutputStream(/*...*/);
closables.add(destStream );
// ... reading from file
// ... processing
// ... writting to file
destStream.flush();
} catch (Exception e) {
Log("ERROR: " + e.getMessage());
} finally {
for(Closable closable : closables)
closeIgnoringException(closable );
}