Java I/O and files

Reading and writing the outside world. The two mistakes that never stop happening are ignoring the charset and leaving a stream open.

3 concepts · 9 interview questions

What this topic covers

Every concept in i/o and files, and the questions each one gets asked as. Where a question links, it has a full write-up.

Byte streams vs character streams

InputStream moves bytes, Reader moves characters, and the bridge between them needs an explicit charset every single time.

  • What is the difference between InputStream and Reader?
  • Why must you always specify a charset?
  • What does buffering actually change?

java.nio.file

Path and Files replaced java.io.File, with real error reporting instead of methods that return false.

  • Why use Path and Files instead of File?
  • How do you read a large file without loading it into memory?
  • What does Files.lines return, and why must you close it?

Serialization

Java's built-in serialization is a documented security liability and a versioning trap; JSON with a library is the answer.

  • Why is Java serialization considered dangerous?
  • What is serialVersionUID for?
  • What does transient do?

More in Core Java

See the whole reference map →