Java / Java File I/O and Serialization

Working with NIO for Advanced File Operations

This tutorial will introduce you to Java's New Input/Output (NIO) APIs. You'll learn how to use these APIs for high-speed, scalable file operations.

Tutorial 5 of 5 5 resources in this section

Section overview

5 resources

Explores reading, writing, and manipulating files in Java.

Introduction

The goal of this tutorial is to provide an understanding of Java's New Input/Output (NIO) APIs, which enable high-speed, scalable file operations. By the end of the tutorial, you will have learned:

  • Basic understanding of Java NIO
  • How to use NIO for file operations
  • Best practices when working with NIO

Prerequisites for this tutorial are a basic understanding of Java and its standard I/O operations.

Step-by-Step Guide

Java NIO provides many features that standard I/O does not, like non-blocking I/O operations, buffer for data manipulation, and channels to provide a connection for I/O operations.

Channels and Buffers

In standard I/O, you work with streams, but in NIO you work with channels and buffers. Data is always read from a channel into a buffer, or written from a buffer into a channel.

try (FileChannel channel = FileChannel.open(Paths.get("file.txt"), StandardOpenOption.READ)) {
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    int bytesRead = channel.read(buffer);
}

In the above code, we first open a FileChannel to the file.txt file. Then we create a ByteBuffer with a capacity of 1024 bytes. The channel.read(buffer) call reads data from the channel into the buffer.

Non-Blocking I/O

Java NIO allows you to do non-blocking I/O operations. This means that you can do other things while waiting for I/O operations to complete.

try (ServerSocketChannel serverChannel = ServerSocketChannel.open()) {
    serverChannel.configureBlocking(false);
    // other operations
}

In the above example, serverChannel.configureBlocking(false) puts the serverChannel into non-blocking mode.

Code Examples

Let's see a couple of examples of how to use Java NIO for file operations.

Reading a File with Java NIO

Path path = Paths.get("file.txt");
try (SeekableByteChannel sbc = Files.newByteChannel(path, StandardOpenOption.READ)) {
    ByteBuffer buf = ByteBuffer.allocate(1024);
    while (sbc.read(buf) > 0) {
        buf.rewind();
        System.out.print(Charset.defaultCharset().decode(buf));
        buf.flip();
    }
}

In this example, we first create a Path to the file.txt file. Then we open a new SeekableByteChannel to the file. We read data from the channel into the buffer until there's no more data to read. When the buffer is full, we rewind it, decode the data into characters, and print them.

Writing to a File with Java NIO

Path path = Paths.get("file.txt");
try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.WRITE)) {
    String data = "Hello, NIO!";
    ByteBuffer buffer = ByteBuffer.wrap(data.getBytes());
    fileChannel.write(buffer);
}

In this example, we first create a Path to the file.txt file. Then we open a new FileChannel to the file. We wrap the data into a ByteBuffer and write it to the file.

Summary

In this tutorial, we have learned about Java NIO, including channels and buffers, and how to perform non-blocking I/O operations. We have also looked at examples of how to read from and write to a file using Java NIO.

For further learning, you may want to explore the Selector in Java NIO, which allows a single thread to manage multiple channels.

Practice Exercises

  1. Write a Java NIO based program to copy content from one file to another.
  2. Write a Java NIO based program that reads a large file and writes it to another file in chunks of 1KB.

You may use the examples in this tutorial as a starting point. Remember, practice is key to mastering any concept.

Solutions

  1. Copying content from one file to another using Java NIO.
Path sourcePath = Paths.get("source.txt");
Path destinationPath = Paths.get("destination.txt");
try (FileChannel sourceChannel = FileChannel.open(sourcePath, StandardOpenOption.READ);
     FileChannel destinationChannel = FileChannel.open(destinationPath, StandardOpenOption.WRITE)) {
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    while (sourceChannel.read(buffer) > 0) {
        buffer.flip();
        while (buffer.hasRemaining()) {
            destinationChannel.write(buffer);
        }
        buffer.clear();
    }
}
  1. Reading a large file and writing in chunks of 1KB using Java NIO.
Path sourcePath = Paths.get("largefile.txt");
Path destinationPath = Paths.get("output.txt");
try (FileChannel sourceChannel = FileChannel.open(sourcePath, StandardOpenOption.READ);
     FileChannel destinationChannel = FileChannel.open(destinationPath, StandardOpenOption.WRITE)) {
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    while (sourceChannel.read(buffer) > 0) {
        buffer.flip();
        while (buffer.hasRemaining()) {
            destinationChannel.write(buffer);
        }
        buffer.clear();
    }
}

In both solutions, we first create Path to the source and destination files. Then we open a FileChannel to each file. We read data from the source file into a buffer, and then write from the buffer to the destination file. We do this until there's no more data to read from the source file.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

JSON Formatter & Validator

Beautify, minify, and validate JSON data.

Use tool

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

Time Zone Converter

Convert time between different time zones.

Use tool

Image Compressor

Reduce image file sizes while maintaining quality.

Use tool

Random Password Generator

Create secure, complex passwords with custom length and character options.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help