Web Analytics

NIO (New I/O)

Advanced ~15 min read

NIO (New I/O) was introduced in Java 1.4 and enhanced in Java 7 (NIO.2). It offers a different way of working with I/O than the standard IO API, including non-blocking IO and a more comprehensive filesystem interface.

Key Components

  • Path: Replaces java.io.File for file paths.
  • Files: Utility class for file operations (copy, move, delete, read, write).
  • Buffers & Channels: Core of non-blocking I/O.

Example: Copying a File

Path source = Paths.get("source.txt");
Path dest = Paths.get("dest.txt");
try {
    Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
    e.printStackTrace();
}

Full Example

Output
Click Run to execute your code