Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Java I/O has gone through java.io, NIO in 1.4, NIO.2 in 7, the UTF-8 default in 18 and virtual threads in 21. What does that history suggest about how your organisation should write I/O code that lasts?

The history shows that Java never removes old I/O APIs, that platform defaults change, and that the best model for scalable I/O has swung from blocking to asynchronous and, with virtual threads, back to simple blocking code. So lasting I/O code standardises on Files and Path with explicit charsets, never depends on defaults such as charset or buffer sizes, prefers plain blocking code on virtual threads over complex async code unless there is a measured need, hides I/O behind small internal interfaces where switching models is likely, and keeps lint rules for default charsets and unclosed resources.

Why this question is asked

Long-lived codebases outlive several generations of the platform they run on. A Java system started in 2005 has seen java.io streams, NIO buffers and channels, NIO.2's Files and Path, the UTF-8 default and virtual threads, and each wave left code behind. Interviewers ask senior engineers this question to hear whether they can read a history as evidence and turn it into practical guidance for an organisation, rather than just listing release features. In this answer, we will walk through the timeline briefly, draw out the patterns it shows, and turn each pattern into a guideline with the reasoning behind it.

1A Short Timeline of Java I/O

The original java.io package arrived with Java 1.0 and 1.1: byte streams, character streams, and the File class. Its model is simple and blocking, one thread per stream. Java 1.4 added NIO, with buffers, channels and selectors, so that one thread could serve many sockets without blocking. Java 7 added NIO.2: the Path and Files API that fixed many weaknesses of File, plus asynchronous channels. Java 18 changed the default charset to UTF-8 on every platform. Java 21 made virtual threads final, which made blocking code cheap again because a blocked virtual thread no longer ties up an operating system thread.

Waves of Java I/O and what each left behind
ReleaseWhat arrivedWhat it taught
1.0 / 1.1java.io streams, Reader/Writer, Filesimple blocking model; File reports errors poorly
1.4NIO buffers, channels, selectorsscale needed non-blocking code, at a complexity cost
7NIO.2 Path, Files, async channelsbetter APIs arrive, old ones stay
18UTF-8 default charset (JEP 400)defaults change under running code
21virtual threads finalsimple blocking code scales again

2Pattern 1: Old APIs Stay, So Styles Accumulate

Java keeps backward compatibility very seriously. java.io.File, FileReader and the original streams are all still there and still work. The benefit is that old code keeps running. The cost is that a large codebase ends up with three or four ways of doing the same thing, each with different error handling and different defaults, and new developers copy whichever style they find first.

The guideline that follows is to choose one modern style and say so. For file work that means Path and Files: Files.readString, Files.newBufferedReader, Files.lines, Files.copy and Files.move. They throw IOException with a reason instead of returning false, support file attributes and atomic moves, and take a charset everywhere. Old code is not rewritten for its own sake, but code that is touched moves to the standard style.

3Pattern 2: Defaults Change, So Do Not Depend on Them

The UTF-8 change in Java 18 broke code that had never mentioned an encoding, because that code depended on a default it did not know about. The same can happen with other implicit values: buffer sizes, timeouts, the platform line separator, the default time zone and locale.

So the guideline is to make the values that define a data format or a behaviour explicit in the code. We pass a charset on every conversion, name the line separator when the file format requires one, and set timeouts on every network client. Performance tuning values, such as a buffer size, can stay at a library's default, but only when the default is not part of correctness.

4Pattern 3: The Best Model for Scale Changes

For years, serving many concurrent connections in Java meant leaving blocking code behind. NIO selectors, asynchronous channels, Netty and reactive frameworks let a few threads handle thousands of connections, but they made code harder to write, read, debug and profile. Stack traces became fragments and simple sequential logic became chains of callbacks.

Virtual threads changed the trade-off. A blocking call on a virtual thread parks the virtual thread and frees its carrier thread, so we can write straightforward blocking code and still handle very high concurrency. The guideline is to default to simple blocking I/O on virtual threads for new services, and to choose reactive or callback-based code only where a measured requirement needs it, such as streaming backpressure or an existing reactive stack. This is also a lesson about humility: the expensive async rewrite that was right in 2015 may be unnecessary in 2026, so we should not lock the whole organisation into one model.

Java
// Simple blocking code, scaled by virtual threads
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (Path p : files) {
executor.submit(() -> {
String text = Files.readString(p, StandardCharsets.UTF_8); // blocks the virtual thread only
return parse(text);
});
}
} // close() waits for all tasks to finish

5Pattern 4: Contain I/O Behind Small Interfaces

Each change in the I/O model was painful in proportion to how widely I/O code was spread. Where file or network access was scattered through business logic, moving to a new API meant touching hundreds of classes. Where it sat behind a small interface, such as a DocumentStore with get and put, the change touched one implementation.

So the guideline is to wrap I/O behind small internal interfaces where a change of model or backend is plausible: storage that may move from local disk to object storage, a client whose transport may change. We do not wrap everything, because a wrapper around a single Files.readString call adds only indirection; we wrap where the boundary is likely to move.

6Pattern 5: Let Tools Hold the Rules

Guidelines decay if they rely on memory and review. Two rules catch most I/O problems mechanically: a check that flags default-charset calls, such as Error Prone's DefaultCharset, and checks for resources that are not closed, such as streams from Files.lines or Files.list outside try-with-resources. We put them into the shared build configuration as errors. That keeps new code on the standard, and when the platform changes again we can add a rule rather than run another fleet-wide hunt.

7Follow-up questions

Should we rewrite our reactive services now that virtual threads exist?

Not by default. A working reactive service that the team understands is not a problem to solve. What changes is the default for new services and for services that are being rewritten anyway, and the bar for introducing reactive code: we now need a concrete reason such as streaming with backpressure, not just a high connection count. Where a team struggles to maintain a reactive service, moving it to blocking code on virtual threads can be a good investment, measured by simpler code and incidents, not by fashion.

Are there cases where virtual threads do not help I/O code?

Yes. Virtual threads help when threads spend their time waiting. Code that holds a lock around blocking I/O could pin the carrier thread in older JDKs; Java 24 removed most synchronized pinning, but native calls and some file system operations still occupy a carrier, so we test under load and check the JDK version. CPU-heavy work also gains nothing, since the limit there is cores, not threads.

8Conclusion

In this answer, we read the history of Java I/O as evidence: old APIs stay, defaults change, and the best model for scale has moved from blocking to async and back. That suggests an organisation should standardise on Files and Path with explicit charsets, avoid depending on defaults, prefer simple blocking code on virtual threads unless a need is measured, contain I/O behind small interfaces where change is likely, and enforce the rules with lint checks.

What to say in the interviewAbout 90 seconds aloud

Open with

What the history tells me is that the platform keeps old APIs forever, changes its defaults, and changes its opinion about the right model for scale, so code that lasts should avoid depending on any of those.

Then, if they want more

Concretely, first, because java.io, NIO and NIO.2 all coexist, codebases collect several styles. I would standardise on Path and Files, which have better error reporting, atomic moves and charset parameters everywhere, and move code to that style when it is touched.

Second, the UTF-8 default in Java 18 showed that defaults can change under running code, so I want explicit charsets on every conversion and explicit values for anything that defines a format or behaviour, like timeouts and line separators. Third, for years scale meant NIO selectors and reactive code, which is hard to read and debug.

Virtual threads in Java 21 make simple blocking code scale again, so for new services I default to blocking code on virtual threads and only choose reactive where there is a measured need such as backpressure.

Fourth, the migrations that hurt most were the ones where I/O was spread everywhere, so I put I/O behind small internal interfaces where the backend or model is likely to change, without wrapping every single call. And finally I let tools hold the rules: lint checks for default charsets and unclosed resources run as build errors in the shared configuration.

About this answer

Last updated
12 November 2026
Versions it names
Java 18, Java 21, Java 24, Java 7
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Java I/O has gone through java.io, NIO in 1.4, NIO.2 in 7, the UTF-8 default in 18 and...": https://scrivio-interview-library.netlify.app/java/io-and-nio/28d682e546/