Javatpoint Logo
Javatpoint Logo

Scatter/Gather or Vectored I/O

In Java NIO the channel provides an important capability known as scatter/gather or vectored I/O. It is a simple yet powerful technique through which the bytes can be written from a set of buffers to a stream using a single write() function and bytes can be read from a stream into a set of buffers using a single read() function.

Java NIO has inbuilt scatter/gather support. It can be used in reading from channels and writing to channels.

Scattering Reads

The 'scattering read' is used for reading the data from a single channel into multiple buffers.

Let's see the illustration of the Scatter principle:

Java Nio Tutorial11

Let's see the code example that performs a scattering read operation:


Gathering Writes

The 'gathering write' is used for writing the data from a multiple buffers into a single channel.

Let's see the simple illustration of the Gather principle:

Java Nio tutorial12

Let's see the code example that performs a gathering write operation:


Basic Scatter/Gather Example

Let's see the simple example of two buffers. The first buffer holds a random number and second buffer holds the data we want to write using scatter/gather mechanism:

Output:

In above program, first buffer print the random output on a console and second buffer print the "Scattering and Gathering example shown in javatpoint.com" on a console.

It also replaces the content of testout.txt file with "Scattering and Gathering example shown in javatpoint.com".






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA