Skip to main content
Module 9 · Lesson 2 of 220/22 in the course~14 min
Module lessons (2/2)

Modern File I/O with NIO.2

Lesson preview5 hands-on exercises readyCourse Java · Module 9 — Concurrency and File I/O (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, and normalize.
  • Read and write text files concisely with Files.readString and Files.writeString specifying StandardCharsets.UTF_8.
  • Analyze large files in lazy mode via Files.lines closed in try-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

  1. 11. Build Path OS Independent
  2. 22. Read and Write Compact Text (Java 11+)
  3. 33. Lazy Streaming for Huge Files (Files.lines)
  4. 44. Browse Directories and Copy Files
  5. 5Try it yourself
Pro Lesson · Module 9 — Concurrency and File I/O (NIO.2)

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.