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.transferTo().

Source Code

1 import java.io.*;
2 import java.nio.channels.*;
3 import java.nio.file.*;
4 public class FileChannelTransferExample {
5 public static void main(String[] args) throws Exception {
6 System.out.println("Eduinq FileChannel Transfer Example");
7 FileChannel src=FileChannel.open(Paths.get("source.txt"),StandardOpenOption.READ);
8 FileChannel dest=FileChannel.open(Paths.get("dest.txt"),StandardOpenOption.WRITE,StandardOpenOption.CREATE);
9 src.transferTo(0,src.size(),dest);
10 src.close(); dest.close();
11 System.out.println("Eduinq File Copied Successfully");
12 }
13 }

Program Output

					Eduinq FileChannel Transfer Example
Eduinq File Copied Successfully
				

Explanation

Uses transferTo() for direct channel copy. - Efficiently copies file content. - Exception handling ensures safe operations. - In enterprise systems, transfer supports fast file duplication. Conclusion: Shows transferring data between channels.

Quick Links to Explore