Java Zero to All All questionsPractise free
Interview questions › Java Foundations — Zero to Basics › File I/O — java.io, NIO2 & Files API

Compare the old java.io streams with NIO.2 (java.nio.file). When do you still reach for streams?

Level II · JuniorFile I/O — java.io, NIO2 & Files API
Short answer

java.io is a blocking, stream-oriented API (InputStream/Reader and their buffered wrappers) that's fine for straightforward sequential reading and writing.

Explanation

NIO.2 (Java 7) adds the Path/Files API with far richer operations: atomic moves, symbolic links, file attributes, directory walking, and clearer exceptions, and it's the modern default for filesystem work. You still use streams for the actual byte/char transfer and for arbitrary sources (sockets, in-memory), but you'd use Files.newInputStream/Files.lines and Path rather than raw FileInputStream and File.

Common mistakes
  • Using java.io.File for new code.
  • Not closing streams, or not using try-with-resources.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for File I/O — java.io, NIO2 & Files API.

Practise free

More on File I/O — java.io, NIO2 & Files API