Module lessons (2/2)
Modern File I/O with NIO.2
Portable paths with Path.of, fast read/write with Files.readString/writeString, and lazy streaming Files.lines.
What you'll learn in this lesson
- Build and normalize portable paths between Windows, macOS, and Linux using
Path.of,resolve, andnormalize. - Read and write text files concisely with
Files.readStringandFiles.writeStringspecifyingStandardCharsets.UTF_8. - Analyze large files in lazy mode via
Files.linesclosed intry-with-resources.
Overview
From Package java.nio.file (introduced as NIO.2 in Java 7 and enhanced in Java 11+), the Path interface represents the logical location of files/directories and the Files utility class offers safe and expressive static methods.
Never concatenate "/" or "\\" by hand into strings: Path.of and .resolve() automatically use the correct separator for the OS the JVM is running on:
Sections covered in this lesson
- 11. Build
PathOS Independent - 22. Read and Write Compact Text (
Java 11+) - 33. Lazy Streaming for Huge Files (
Files.lines) - 44. Browse Directories and Copy Files
- 5Try it yourself
Unlock "Modern File I/O with NIO.2" and the full Course Java curriculum
Portable paths with `Path.of`, fast read/write with `Files.readString`/`writeString`, and lazy streaming `Files.lines`.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Java plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.