Java NIO Channel

In Java NIO, channels are used for I/O transfers. Channel is a like a tube that transports data between a buffer and an entity at other end. A channel reads data from an entity and places it in buffer blocks for consumption. Similarly, we should write to buffer blocks and that data will be transported by the channel to the other end.

Channels are the gateway provided by Java NIO to access the native I/O mechanism. We should use buffers to interact with the channels, so the channel is like a bridge between two entities to do the I/O. Buffers are the endpoints provided by channels to send and receive data.


Channel Characteristics

Java NIO Channel Classes

Following are the two major types of Channels classes provided as implementation in Java NIO package.

Java NIO Channel Example

Following example reads from a text file and prints the content to the console. We have used RandomAccessFile and FileChannel to read the file into a ByteBuffer. We read 512 bytes of data into buffer then invoke flip method to make it ready for the get operation. On get we print the content to console the clear the buffer to keep it ready for the next sequence of read. This keeps on continuing until the end of file.


import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class ChannelExample {
	public static void main(String args[]) throws IOException {
		RandomAccessFile file = new RandomAccessFile("temp.txt", "r");
		FileChannel fileChannel = file.getChannel();
		ByteBuffer byteBuffer = ByteBuffer.allocate(512);
		while ( > 0) {
			// flip the buffer to prepare for get operation
			while (byteBuffer.hasRemaining()) {
				System.out.print((char) byteBuffer.get());
			// clear the buffer ready for next sequence of read

We can see about NIO Buffer and each of these Channel classes in the coming tutorial.

This Java tutorial was added on 20/08/2015.

Comments on "Java NIO Channel" Tutorial:

  1. […] Buffer is one of the main differences between the old Java I/O and the NIO. Previously data is read directly from a stream or written directly into it. Now the data is read from a buffer or written into it. Channels are synonymous to streams in the NIO. To know more about NIO channel, please read the previous tutorial Java NIO Channel. […]

  2. Firoz says:

    Hi Joe,
    I am waiting for some article on Material Designing in android. Hope It will be here soon.


  3. Jono says:

    Few more things to rbemmeer while using Memory Mapped File for high performance application :1) Prefer Direct Byte buffer over Non Direct Buffer2) Don’t call MappedByteBuffer.force() method to often, this method is meant to force operating system to write content of memory into disk, So if you call force() method each time you write into memory mapped file, you will not see true benefit of using mapped byte buffer, instead it will be similar to disk IO.3) In case of power failure or host failure, there is slim chance that content of memory mapped file is not written into disk, which means you could lose critical data.Cheers

Comments are closed for this "Java NIO Channel" tutorial.