I/O streams
I/O streams are sequential sources and sinks of bytes or characters. InputStream/OutputStream move bytes. Reader/Writer move characters. Close them with try-with-resources.
Detailed Explanation
A file, a socket, and a compressed blob are not random-access arrays in your program. You read the next bytes, then the next. java.io models that as streams. Byte streams (InputStream, OutputStream) are the base. Character streams (Reader, Writer) decode and encode text with a Charset.
Decorators wrap streams to add behaviour. BufferedInputStream reads a block at a time so you do not make a system call per byte. GZIPInputStream inflates. InputStreamReader turns bytes into chars. BufferedReader adds readLine. The order of wrapping matters: buffer close to the raw stream, then decode, then interpret lines.
Always set the charset when you convert bytes to text. new FileReader(file) used the platform default for years; FileReader now has a charset constructor, and Files.newBufferedReader(path, UTF_8) is clearer. PrintStream.println is convenient and still a byte stream with a charset (UTF-8 for System.out on modern JDKs, but do not assume that in a file you open yourself).
These streams are not the Streams API (java.util.stream). The word collision is historical. I/O streams are pull-based, blocking, and closeable. They are also mostly one-pass: once you have read a byte, it is gone unless you buffered or marked it.
You reach for java.io when wrapping an existing InputStream (a servlet request, a socket, a classpath resource). You reach for java.nio.file.Files and NIO channels when you have paths, need directory walks, or want memory-mapped files. Files.readString and Files.copy hide a lot of stream plumbing for common cases.
The cost of unbuffered byte-at-a-time I/O is extreme slowness. The cost of not closing is leaked descriptors. The cost of the wrong charset is corrupted text that looks fine on your laptop. Prefer try-with-resources, buffer, name UTF-8, and do not mix a Writer and a raw OutputStream on the same destination without flushing the writer first.
Code Examples
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class ClasspathRead {
static String firstLine() throws IOException {
InputStream in = ClasspathRead.class.getResourceAsStream("/app.properties");
if (in == null) {
return "";
}
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
String line = reader.readLine();
return line == null ? "" : line;
}
}
}import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class MemoryIo {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write("hi".getBytes(StandardCharsets.UTF_8));
byte[] bytes = out.toByteArray();
try (var in = new ByteArrayInputStream(bytes)) {
System.out.println(in.read()); // prints: 104 ('h')
}
}
}Common Gotchas
Platform default charset differs by OS
always pass `StandardCharsets.UTF_8` when encoding or decoding text.
Forgetting to close leaks file descriptors
use try-with-resources on the outermost wrapper; it closes the nested stream.
`PrintWriter` swallows I/O errors
check `checkError()` or use a `Writer` that throws.
`java.util.stream` is a different API
do not expect `map`/`filter` on an `InputStream`.