Java NIO Tutorial (with Examples)

Java NIO (New IO) is an alternative IO API for Java (from Java 1.4), meaning alternative to the standard Java IO API’s. Java NIO offers a different way of working with IO than the standard IO API’s. In this page, I will list down all available posts related to NIO, available in this blog.

java nio

 Pre-requisites for Learning NIO

How Java I/O Works Internally at Lower Level?

This blog post mostly talks about how things related to I/O work at the lower level. This post is intended for readers who are curious to know that how java I/O operations are mapped at machine level; and what all things hardware does all the time when your application is running. I am assuming that you are familiar with basic IO operations such as reading a file, writing a file through java I/O APIs; because that is out of scope of this post.

Difference between standard IO and NIO

In this tutorial, I will focus on identifying most noticeable differences which you must know before deciding which one to use in your next project.

NIO Basics

How to define Path in java NIO

If your application uses NIO, you should learn more about the powerful features available in this class. In this tutorial, I am listing 6 ways to create Path in NIO.

NIO Buffers

Buffer classes are the foundation upon which java.nio is built. In this tutorial, we’ll take a closer look at buffers, discover the various types, and learn how to use them. We’ll then see how the java.nio buffers relate to the channel classes of java.nio.channels.

NIO Channels

Channels are the second major innovation of java.nio after buffers which we have learned in my previous tutorial in detail. Channels provide direct connections to I/O services. A Channel is a medium that transports data efficiently between byte buffers and the entity on the other end of the channel (usually a file or socket). Usually channels have a one-to-one relationship with operating-system file descriptors. The channel classes provide the abstraction needed to maintain platform independence but still model the native I/O capabilities of modern operating systems. Channels are gateways through which the native I/O services of the operating system can be accessed with a minimum of overhead, and buffers are the internal endpoints used by channels to send and receive data.

How to Use NIO in Your Application

Read file line by line using NIO

In this post, I am giving example of a very useful task in day to day programming i.e. reading files line by line using java IO and perform some operations of lines. Before moving ahead, let me mention the file content which I will read in all examples in this post.

I will read the content of file line by line and check if any line contains word "password" then print it.

3 ways to read files using Java NIO

In this post, I am showing a couple of ways to read a file from file system.

How to transfer data between channels?

As in normal java applications where IO happens mostly between an input source and output target, in NIO as well you may need to transfer data from one channel to another channel very frequently. Bulk transfers of file data from one place to another is so common that a couple of optimization methods have been added to the FileChannel class to make it even more efficient.

Lets learn about these methods.

Memory-Mapped Files and MappedByteBuffer

Memory-mapped I/O uses the filesystem to establish a virtual memory mapping from user space directly to the applicable filesystem pages. With a memory-mapped file, you can pretend that the entire file is in memory and that you can access it by simply treating it as a very large array. This approach greatly simplifies the code you write in order to modify the file.

Scatter/Gather or Vectored IO

A scattering read from a channel is a read operation that reads data into more than one buffer. Thus, the channel “scatters” the data from the channel into multiple buffers. A gathering write to a channel is a write operation that writes data from more than one buffer into a single channel. Thus, the channel “gathers” the data from multiple buffers into one channel. Scatter / gather can be really useful in situations where you need to work with various parts of the transmitted data separately.

References:

http://docs.oracle.com/javase/tutorial/essential/io/fileio.html

 

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.