Consider the following sequence of events
public static void main(String[] a) throws IOException {
FileReader f = new FileReader(a[0]);
System.out.println(f.ready()); // true
doSomethingWithReader(f);
System.out.println(f.ready()); // false
}
private static String doSomethingWithReader(FileReader reader) {
BufferedReader br = null;
try {
br = new BufferedReader(reader);
...
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return line;
}
FileReader is not closed anywhere, but what closes it?
When you wrap a
FileReader(or anyReader) with aBufferedReader, calling.close()on theBufferedReaderwill close the wrapped/underlyingFileReaderas well.This is true of all the standard Reader, Writer, OutputStream, and InputStream classes that can be used as wrappers.