Thursday 6 June 2019

NIO: Exploring Channel


A channel represents an open connection to an entity like a file, socket etc., Channels are bi-directional, whereas streams are in one-direction. You can open a channel for reading, writing (or) for both.

'java.nio.channels.Channel' interface represents an interface, that is responsible to perform IO operations.

Classes like FileChannel, DatagramChannel, SocketChannel implement Channel interface and these are abstract classes.

How to create a FileChannel?
FileInputStream fin = new FileInputStream(filePath);
FileChannel fileChannel = fin.getChannel();

In my coming posts, I am going to explain
a.   How to read data from a file using FileChannel
b.   How to write data to a file using FileChannel
c.    How to perform Read and write together using FileChannel

Note
A channel can only be read/write from a ByteBuffer.

Previous                                                 Next                                                 Home

No comments:

Post a Comment