try-with-resources closes in reverse order
Java I/O is built on two families: byte streams (InputStream/OutputStream) move raw 8-bit bytes, and character streams (Reader/Writer) move text. A Reader or Writer is a byte stream plus a charset that turns bytes into characters and back.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write static long countBytes(InputStream in) that reads in 4-byte chunks and returns the total number of bytes, then test it on a ByteArrayInputStream of 10 bytes. Print how many chunks were read too.
Encode the word "r\u00e9sum\u00e9" (resume with two accents) to UTF-8 and ISO-8859-1, and print both byte counts and whether decoding the UTF-8 bytes as ISO-8859-1 gives back the same string.
Use DataOutputStream to write three (String name, int score) pairs into a ByteArrayOutputStream, then read them back with DataInputStream until EOFException and print each pair.
Explain it without notes
What's the difference between a byte stream and a character stream, and when do you use each?
Why does InputStream.read() return an int instead of a byte?
How do InputStreamReader and OutputStreamWriter fit in, and why should you always pass a charset?
Explain the Decorator pattern in java.io with an example.
What guarantees does try-with-resources give, and what happens if both the body and close() throw?
Expected output
body finished
closing B
closing A
-- wrapped --
closing outer
closing inner
sink holds: xyz