Java I/O Basics
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
Java I/O is the set of Java APIs used to receive and send data. The data may come from a file, socket, memory array, console, or another stream. Common interfaces let the same kind of read or write logic work with different sources and destinations.
The first choice is bytes or text. InputStream and OutputStream move raw bytes, so they suit images, ZIP files, and binary protocols. Reader and Writer move characters and use a charset such as UTF-8 to decode or encode text. Wrappers can then add features such as buffering, primitive values, or formatted output.
For example, Files.readString(path, UTF_8) is convenient for a small UTF-8 configuration file. A very large log should be processed through a buffered reader one part at a time so the whole file is not held in memory. In both cases the program chooses the source, opens the right abstraction, reads or writes the data, handles errors, and closes the resource.
Modern file code usually represents locations with Path and performs operations with Files. The older File class is still common at library boundaries, while NIO channels and buffers support needs such as random access and some non-blocking APIs. Safe I/O code chooses bytes versus text deliberately, uses bounded memory, and makes resource ownership and closing clear.
Java I/O Basics
Direct Answer
Java I/O is the standard set of APIs for moving data between a Java program and sources or destinations such as files, network connections, memory, and the console. It provides reusable byte, character, buffering, path, and resource-management abstractions.
Deep Dive & Architecture
A Path is a location understood by a file-system provider. It does not open the file and the file need not exist. Opening a stream, reader, writer, or channel creates a live resource through which data moves. The bytes or characters are the payload. Keeping these three ideas separate prevents mistakes such as treating a pathname check as a guarantee that a later open will succeed.
I/O calls can fail for reasons outside the program: permissions change, storage fills, a network peer disconnects, or another process replaces a file. Good code handles IOException at a boundary that can retry, report, compensate, or stop safely rather than pretending every failure means “not found.”
Code Example
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
class JavaIoOverview {
public static void main(String[] args) throws Exception {
Path file = Files.createTempFile("io-overview-", ".txt");
try {
Files.writeString(file, "Java I/O moves data.", StandardCharsets.UTF_8);
String text = Files.readString(file, StandardCharsets.UTF_8);
System.out.println(text);
} finally {
Files.deleteIfExists(file);
}
}
}
Check your understanding
Answer these without looking back at the article.
- When should you use a byte stream instead of a reader?
- Why should text code specify a charset?
- What is the difference between a Path and an open stream?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.