Is there a not so ugly way of treat the close()
exception to close both streams then:
InputStream in = new FileInputStream(inputFileName);
You could implement a utility method:
public final class IOUtil {
private IOUtil() {}
public static void closeQuietly(Closeable... closeables) {
for (Closeable c : closeables) {
if (c != null) try {
c.close();
} catch(Exception ex) {}
}
}
}
Then your code would be reduced to:
try {
copy(in, out);
} finally {
IOUtil.closeQuietly(in, out);
}
Additional
I imagine there'll be a method like this in a 3rd party open-source library. However, my preference is to avoid unnecessary library dependencies unless I'm using a large portion of its functionality. Hence I tend to implement simple utility methods like this myself.