0

Consider the typical code:

try {
    FileReader fr = new FileReader("42.txt");
    BufferedReader br = new BufferedReader(fr);
} finally {
        //??
}

Should close in the finally clause be called for both or them or is it sufficient to close only one reader (if so, then which one?).

I would think that it should be sufficient to close only the most external wrapper, because with the following code that utilises try with resources only br will be closed:

try (BufferedReader br = new BufferedReader(new FileReader("42.txt"))) {
} 
lukeg
  • 4,189
  • 3
  • 19
  • 40

1 Answers1

1

The outermost one. It will then call close() on the wrapped stream etc. if it's implemented correctly (which is certainly true for JDK classes).

Kayaman
  • 72,141
  • 5
  • 83
  • 121