FileChannel Transfer Example

This program demonstrates transferring data between channels.

Problem Statement

Write a Java program that copies data from one file to another using FileChannel transfer.

Source Code

1 import java.io.*;
2 import java.nio.channels.*;
3 public class FileChannelTransferExample {
4 public static void main(String[] args) {
5 System.out.println("Eduinq FileChannel Transfer Example");
6 try(FileChannel src=new FileInputStream("source.txt").getChannel();
7 FileChannel dest=new FileOutputStream("dest.txt").getChannel()) {
8 src.transferTo(0,src.size(),dest);
9 System.out.println("Eduinq File Copied Successfully");
10 } catch(Exception e){ System.out.println("Eduinq Exception: "+e.getMessage()); }
11 }
12 }

Program Output

					Eduinq FileChannel Transfer Example
Eduinq File Copied Successfully
				

Explanation

Uses transferTo() for direct copy. - Efficient zero-copy transfer. - Exception handling ensures safe operations. - In enterprise systems, transfer optimizes file copy. Conclusion: Shows transferring data between channels.

Quick Links to Explore