Introduction to Java NIO…

Seyed Sahil
3 min readJan 30, 2021

Hello there and welcome to my new article on Java Programming. Today we will talk about Java NIO which stands for New I/O. Going forward we will talk about some of the useful classes and use of foundational components — Channels and Buffers.

The Path Interface

Being one of the most important addition to NIO, it encapsulates a File location in a given file system. The operations defined on Path interface doesn't interact with the actual file system. To get the real path encapsulated by the Path object, we have to call toRealPath() method which will throw IOException if the the actual file doesn't exist in the file system. We cannot instantiate a Path object directly, for this we have to use one of the static methods from Paths class. Take a look at the below given code and its output.

Path 'movies.txt' is relative
Path 'C:\Users\seyed\Home\My Space\Development\Research\image-clean\movies.txt' is absolute

The Files Class

The Files class contain methods which is used to perform several operations pertaining to a File. The File to be acted upon is specified using its Path. Some of these operations are create, delete, move copy etc. and it also got other methods which tells us the attribute level information. Take a look at the below given code and its output.

File 'package1.txt' copied from 'source' to 'target'
File 'package1.txt' deleted from 'source'
Directory 'older' created in 'target'
File 'package2.txt' moved from 'target' to 'target\older'

The operations mentioned in the above given program interacts with the file systems and so they will throw exceptions if there is any bad operations.

Channels and Buffers

An open connection to an I/O device is called Channel and Buffer is used to hold the data. Here, a perfect example of I/O device is a file. To the base a Buffer is defined by three properties - current position, limit and capacity where current position indicates the position to which next read / write operation happen. Capacity indicates the maximum elements a Buffer can hold and most of the time limit will be N where N stands for the index of first element that should not be read or written. It is always possible to change the limit of a buffer and not cannot be changed beyond capacity.

In Java, several objects supports channels and they are FileInputStream, FileOutputStream, Socket etc. and to get the channel we have to call getChannel() method from these objects. Channels are auto closeable and also provide us with methods that gives us access and control over the channel.

Data is read from a Channel into a Buffer and written from a Buffer to a Channel.

Read File Contents…

Lets see how to make use of channels and buffers to read contents from a file. Here in the below given program I have included few of the scenarios with comments. The file otp.txt contains a long chain of digits in random order.

 After allocation - Buffer[capacity=12, limit=12, position=0]
Stage 1
Number of bytes read from file is '12'
After loading data - Buffer[capacity=12, limit=12, position=12]
After rewind - Buffer[capacity=12, limit=12, position=0]
New OTP '123456' created.
After reading OTP - Buffer[capacity=12, limit=12, position=6]
Stage 2
After flip - Buffer[capacity=12, limit=6, position=0]
New OTP '123456' created.
After reading OTP - Buffer[capacity=12, limit=6, position=6]
Stage 3
After increasing limit - Buffer[capacity=12, limit=12, position=6]
New OTP '768900' created.
After reading OTP - Buffer[capacity=12, limit=12, position=12]
Stage 4
After loading data to buffer, caret position in file is '0'
After new file read - Buffer[capacity=12, limit=12, position=12]
After rewind - Buffer[capacity=12, limit=12, position=0]
New OTP '123456' created.
After reading OTP - Buffer[capacity=12, limit=12, position=6]
After flip - Buffer[capacity=12, limit=6, position=0]
Number of bytes read from file is '6'
After new file read - Buffer[capacity=12, limit=6, position=6]
New OTP '856784' created.
After reading OTP - Buffer[capacity=12, limit=6, position=6]

Yeah, this one is a bity lengthy program and here I tried to include various operations and behavior of Buffer as well as Channel upon executing different operations.

With this the introduction to Java NIO is complete.

Thank You

Seyed Sahil

Originally published at http://sydlabz.wordpress.com on January 30, 2021.

--

--

Seyed Sahil

Coding Since 2011, Software Engineer, Game Developer, Artist, Photographer. Passionate about Security and Web Technologies. Favourites — C, Java, Javascript.