Your DataOutputStream inherits its close() -method from FilterOutputStream , which the documentation claims to be:
Closes this output stream and frees up any system resources associated with the stream.
The close method of the FilterOutputStream method calls the flush method and then calls the method to close its main output stream.
The same should be true for all Writer objects (although this is not indicated in the documents).
To avoid memory issues when working with threads in Java, use this template:
// Just declare the reader/streams, don't open or initialize them! BufferedReader in = null; try { // Now, initialize them: in = new BufferedReader(new InputStreamReader(in)); // // ... Do your work } finally { // Close the Streams here! if (in != null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } }
This looks less messy with Java7 because it introduces an AutoCloseable -interface that is implemented by all Stream / Writer / Reader classes. See tutorial .
source share