Java NIO examples to illustrate channels and buffers.

    As the earlier blog gives you a basic knowledge about java NIO. Here, is the some examples to understand the concept of channels and buffers.

Channel is a foundational construction. It helps you to connect the input, output, hardware devices, files and sockets.

Buffer: it is a storage place where we can store the temporary data.

Let us create a channel for processing file reading and writing operations. Make it connected with a buffer.

File Reading Example using java NIO:

  • ·       It includes the built in packages.
  • ·       A public class is created with main() function.
  • ·       Three objects are created. FileInputStream,FileChannel and ByteBuffer.
  • ·       Data is read through the channel and gets stored in buffer.
  • ·       Until the buffer has data, it reads the data and display it in the output screen
  • ·       Finally, all file objects are closed.

Program:

import java.io.FileInputStream;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

public class FileReadEg {

    public static void main(String[] args) throws Exception {

        FileInputStream fInStream = new FileInputStream("example.txt");

        FileChannel fChannel = fInStream.getChannel();

        ByteBuffer buff = ByteBuffer.allocate(1024);

        int bRead = fChannel.read(buff);

        while (bRead != -1) {

            buff.flip();

            while (buff.hasRemaining()) {

                System.out.print((char) buff.get());

            }

            buff.clear();

            bRead = fChannel.read(buff);

        }

       fChannel.close();

        fInStream.close();

    }

}

Output:

‘example.txt’

It is the time read the data

Output in the command prompt…

C:\raji\blog>javac FileReadEg.java

C:\raji\blog>java FileReadEg

It is the time read the data

Next part is writing the data into file.

Writing data into the file:

This program starts with including the built-in packages.

  • A public class is created with main() function.
  • Here, three objects are created foe FileOutputStream, FileChannel and ByteBuffer.
  • FileOutputStream opens the file.
  • FileChannel gets the channel for the output stream.
  • ByteBuffer is used to allocate the memory.
  • Data is created and stored the size in the buffer.
  • Until the buffer size, it writes the data to the file.
  • Finally, the FileChannel and fileOutputStream objects are closed.

Program:

import java.io.FileOutputStream;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

public class FileWriteEg {

    public static void main(String[] args) throws Exception {

        FileOutputStream fOpStream = new FileOutputStream("example.txt");

        FileChannel fChannel = fOpStream.getChannel();

        ByteBuffer buff = ByteBuffer.allocate(1024);

        String data = "Have a nice day";

        buff.put(data.getBytes());

        buff.flip();

        while (buff.hasRemaining()) {

            fChannel.write(buff);

        }

        fChannel.close();

        fOpStream.close();

    }

}

Output:

Output in the command prompt:

C:\raji\blog>javac FileWriteEg.java

C:\raji\blog>java FileWriteEg

Output in the file:

‘example.txt’

Have a nice day

These are simple ways to illustrate channels and buffers in Java NIO. Keep Coding!!!!

No comments:

Post a Comment