It turns out that almost nobody closes resources in Java correctly. Programmers either do not use try-finally
block at all, or just put resource.close()>
If your only problem with IOUtils.closeQuietly
is that it ignores exceptions on OutputStreams, then you can either simply call close()
on them, or create your own utility class which automatically treats the two differently, like this:
public static void close(Closeable resource)
{
try
{
resource.close();
}
catch(Exception e)
{
//swallow exception
}
}
public static void close(OutputStream o)
{
//throw any exceptions
o.close();
}
The correct overloaded method will be selected at compile time in all common situations, although if you're passing OutputStream
s around as Closeable
s then you'll have to change this to do a dynamic instanceof
check to make sure OutputStream
s always throw exceptions.