You should try and close all resources in the reverse order in which you have opened them. Here you try and close only one.
If you have Java 7, use the try-with-resources statement. If you don't, the best way to do that is using Guava's Closer
(available since version 14.0):
final Closer closer = Closer.create();
final FileOutputStream out;
final OutputStreamWriter writer;
final BufferedWriter buffered;
try {
out = closer.register(new FileOutputStream(file), encoding));
writer = closer.register(new OutputStreamWriter(out));
buffered = closer.register(new BufferedWriter(writer));
// do stuff
} catch (IOException e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
If you don't have Closer
, it is easy enough to recreate.