How to use OutputStream within try-with-resources properly?
java, outputstream
Solution
The try construct closes `ostr` at the end. Closing is propagated to `System.out`. A subsequent call to `System.out.println("hmmm");` will bring `System.out` into trouble - but not throw an exception. (That is the strange way `PrintStream`s handle errors.) Try this:
System.out.close();
System.out.println("hmmm");
System.err.println("System.out in trouble: " + System.out.checkError());
This prints (through the still intact System.err stream):
System.out in trouble: true
Problem
I have a simple piece of code, that works not the way I expect it to: ``` try (OutputStream ostr = new BufferedOutputStream(System.out)) { ostr.write("lol".getBytes()); } System.out.println("hmmm"); ``` It results in just ``` lol ``` printed, but not `hmm`. What am I doing wrong? Am I correct assuming that `hmm` isn't being printed because `ostr` closes `System.out` as well? I understand that this is a pretty synthetic example, but I'm still expecting for an answer.