Unravel โบ Java concepts โบ I/O and files
Files and I/O
Java reads and writes files with Path and Files (modern) or stream classes (older).
Learn these first
At a glance
Using a library book: open it, read or write, and return it so others can use it, in a known language.
- 1Files.readString(path), Files.readAllLines(path) and Files.writeString(path, text) handle small filesFiles.lines(path) streams a large file and must be closed.
- 2Path.of("data", "in.txt") builds paths portablyPrefer Path over the older File.
- 3Streams are bytes; readers and writers are charactersWrap a stream in a reader with an explicit charset: new InputStreamReader(in, StandardCharsets.UTF_8).
- Reading a whole large file into memory causes OutOfMemoryError; stream it
- Streams are decorators layered on a native file handle; BufferedReader reads big chunks and serves lines from memory
Path file = Path.of("data", "input.txt");List<String> lines = Files.readAllLines(file, StandardCharsets.UTF_8);try (Stream<String> big = Files.lines(Path.of("big.log"))) { long errors = big.filter(l -> l.contains("ERROR")).count();}- line 1 uses Path, the core of Files and I/O
- line 2 uses Files, the core of Files and I/O
- line 3 uses Files, the core of Files and I/O
Read the full explanation (about 1 min)
Java reads and writes files with Path and Files (modern) or stream classes (older). Always say which character encoding you mean and close what you open.
A mental model
Using a library book: open it, read or write, and return it so others can use it, in a known language.
How it works
Files.readString(path), Files.readAllLines(path) and Files.writeString(path, text) handle small files. Files.lines(path) streams a large file and must be closed.
Path.of("data", "in.txt") builds paths portably. Prefer Path over the older File.
Streams are bytes; readers and writers are characters. Wrap a stream in a reader with an explicit charset: new InputStreamReader(in, StandardCharsets.UTF_8).
Going deeper
Reading a whole large file into memory causes OutOfMemoryError; stream it. User-supplied file names can contain .. and escape your directory: normalise and check the resolved path (path traversal). Buffer reads and writes; unbuffered byte-by-byte I/O is very slow.
Under the hood
Streams are decorators layered on a native file handle; BufferedReader reads big chunks and serves lines from memory. Files methods use NIO channels.
Common mistakes
- Platform-default encodings.
- Not closing streams.
- Loading huge files in one go.
- Trusting user-supplied paths.
How Unravel recognises it
When you paste code, Unravel looks for these signs of files and i/o. Spotting them yourself is a good habit when you read unfamiliar code:
- Types:
FilesPathPathsBufferedReaderBufferedWriterFileReaderFileWriterInputStreamOutputStreamFileInputStreamFileOutputStreamScannerFileInputStreamReaderand 1 more
Related concepts
Builds on this
Frequently asked questions
What is Files and I/O?
Java reads and writes files with Path and Files (modern) or stream classes (older). Always say which character encoding you mean and close what you open.
What are the most common mistakes with Files and I/O?
Platform-default encodings. Not closing streams. Loading huge files in one go. Trusting user-supplied paths.
How can I see Files and I/O in real code?
Paste your own Java into Unravel and it will point out where files and i/o appears, explain how it works at the detail level you choose, and draw the flow of the code around it.