Command Palette

Search for a command to run...

Phase 12Intermediate13 of 17 in Core Java

Files, I/O and Dates

Byte and character streams, buffering, Path and Files (NIO.2), why Java serialization is dangerous, and correct dates and times with java.time.

Almost every real program reads something in and writes something out: a config file, a CSV upload, a network response, a log. Java's I/O library looks big, but it is built from a few small ideas. Everything is a stream of bytes; text is bytes plus an encoding; and small classes wrap other classes to add features like buffering or decoding. Once you see those three ideas, the forty classes in java.io stop being scary.

This phase starts with byte streams and character streams (Topic 12.1), then buffered readers and writers (Topic 12.2), the everyday way to read text line by line. Topic 12.3 moves to the modern file API, **Path and Files (NIO.2, Java 7, with handy additions in Java 11), which is what you should use for files today. Topic 12.4 explains Java's built-in serialization, why it has caused so many security incidents, and what to use instead. Topic 12.5 teaches java.time** (Java 8): the right way to handle dates, times, time zones and durations.

The runnable examples use in-memory streams (ByteArrayInputStream, StringReader and friends) and fixed dates, so they print the same thing every time and work in the browser. Real file code is shown as code you run on your own machine, with the output you should see.

0/5 · 0%
5 topics ~2 h 25 code blocks & diagrams
Start with the first topic
1
12.1

Byte Streams and Character Streams

Java I/O is built on two families: byte streams (InputStream/OutputStream) move raw 8-bit bytes, and character streams (Reader/Writer) move text. A Reader or Writer is a byte stream plus a charset that turns bytes into characters and back.

34 min 5 code practice

2
12.2

Buffered Readers and Writers

BufferedReader and BufferedWriter put an in-memory array between your code and the real source, so thousands of tiny reads or writes become a few big ones. BufferedReader also gives you readLine() and lines(), the standard way to read text line by line.

31 min 6 code practice

3
12.3

Path and Files (NIO.2)

Path is an object that names a file or folder, and Files is a class of static methods that act on the file system: read, write, copy, move, delete, list and inspect. Together (NIO.2, Java 7, with Files.readString/writeString and Path.of added in Java 11) they replace the old java.io.File for all new code.

32 min 5 code practice

4
12.4

Serialization and Why to Avoid It

Java serialization turns an object graph into bytes (ObjectOutputStream) and rebuilds it later (ObjectInputStream). It's built in and easy, but it's fragile across versions and reading untrusted bytes is a classic security hole, so modern code prefers explicit formats like JSON and uses filters when it must deserialize.

20 min 4 code practice

5
12.5

Dates and Times with java.time

java.time (Java 8) replaced the old, error-prone Date and Calendar with clear immutable types: LocalDate and LocalDateTime for calendar values, Instant for a moment on the timeline, ZonedDateTime for a moment in a particular time zone, and Duration/Period for amounts of time.

25 min 5 code practice