Gather Write Example

This program demonstrates gather write using FileChannel.

Problem Statement

Write a Java program that writes data from multiple buffers using gather write.

Source Code

1 import java.io.*;
2 import java.nio.*;
3 import java.nio.channels.*;
4 import java.nio.file.*;
5 public class GatherWriteExample {
6 public static void main(String[] args) throws Exception {
7 System.out.println("Eduinq Gather Write Example");
8 FileChannel channel=FileChannel.open(Paths.get("gather.txt"),StandardOpenOption.WRITE,StandardOpenOption.CREATE);
9 ByteBuffer header=ByteBuffer.wrap("Eduinq Header ".getBytes());
10 ByteBuffer body=ByteBuffer.wrap("Eduinq Body".getBytes());
11 ByteBuffer[] buffers={header,body};
12 channel.write(buffers);
13 channel.close();
14 System.out.println("Eduinq Data Written Successfully");
15 }
16 }

Program Output

					Eduinq Gather Write Example
Eduinq Data Written Successfully
				

Explanation

Uses gather write to combine buffers. - Writes header and body sequentially. - Exception handling ensures safe operations. - In enterprise systems, gather write supports structured output. Conclusion: Shows gather write with FileChannel.

Quick Links to Explore