Skip to main content

Understanding Channels in NIO

Channel

In NIO, a connection is represented by a Channel. As we know, from a broader perspective, a channel can represent an underlying file descriptor, such as hardware devices, files, network connections, etc. However, it goes far beyond that. Besides corresponding to underlying file descriptors, Java NIO channels can be more refined. For example, different network transport protocol types have different NIO Channel implementations in Java.

Main Types of Channels

Without going into too much detail about the complex Java NIO channel types, we'll focus on introducing the four most important Channel implementations: FileChannel, SocketChannel, ServerSocketChannel, and DatagramChannel. For these four channels:

  • (1) FileChannel - File channel for reading and writing file data.
  • (2) SocketChannel - Socket channel for reading and writing data over TCP socket connections.
  • (3) ServerSocketChannel - Server socket channel (or server listening channel) that allows us to listen for TCP connection requests and create a SocketChannel for each listened request.
  • (4) DatagramChannel - Datagram channel for reading and writing UDP protocol data. These four channels cover file IO, TCP network, and UDP IO basics. Below we'll briefly introduce the four channels through four important operations: getting, reading, writing, and closing channels.

FileChannel

FileChannel is specifically for operating on files. Through FileChannel, you can both read data from a file and write data to a file. Special note: FileChannel is blocking mode and cannot be set to non-blocking mode. Below we'll introduce FileChannel's four operations: getting, reading, writing, and closing.

1. Getting a FileChannel

You can get a FileChannel through file input/output streams:

FileInputStream fis = new FileInputStream(new File("C:\\Users\\asher\\Desktop\\test.txt"));
// Convert inputStream to fileChannel
FileChannel inChannel = fis.getChannel();
// Declare byteBuffer, write channel data into byteBuffer
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
inChannel.read(byteBuffer);
// Switch from write mode to read mode
byteBuffer.flip();
byte[] inBytes = new byte[1024];
// Write byteBuffer into byte array
byteBuffer.get(inBytes);
// Output
System.out.println(new String(inBytes));

You can also get a FileChannel through RandomAccessFile:

// Create RandomAccessFile random access object
RandomAccessFile aFile = new RandomAccessFile("filename.txt", "rw");
// Get file stream channel
FileChannel inChannel = aFile.getChannel();