FileChannel
Copying binary file with FileChannel
With this example we demonstrate how to copy files using FileChannels in Java. In particular we are going to read data from a specific file in the file system and write them to another file. In short what we do is the following :
- For the source file we create aFileChannel so as to be able to read data from. To do so you can create aFileInputStream object to encapsulate the target file. Then use the
getChannel()API method of the FileInputStream object to get the file channel - For the destination file we create aFileChannel so as to be able to write data to. To do so you can create aFileOutputStream object to encapsulate the target file. Then use the
getChannel()API method of the FileOutputStream object to get the file channel - To read a sequence of bytes from the source channel and write them to the destination channel all you have to do is use the
transferFrom(ReadableByteChannel, long, long)API method of the destination file’s FileChannel providing the source file’s FileChannel as the input attribute
as described in the code snippet below.
Do not forget to close the channel after you are done processing the file so as to release operating system resources.
Want to be a Java NIO Master ?
Subscribe to our newsletter and download the JDBCUltimateGuideright now!
In order to help you master Java NIO Library, we have compiled a kick-ass guide with all the major Java NIO features and use cases! Besides studying them online you may download the eBook in PDF format!

Thank you!
We will contact you soon.
package com.javacodegeeks.snippets.core;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.nio.channels.FileChannel;public class CopyingBinaryFileWithFileChannel {public static void main(String[] args) {try { // source file channel// return the unique FileChannel object associated with this file input stream. FileChannel srcChannel = new FileInputStream("src.dat").getChannel(); // destination file channel // return the unique FileChannel object associated with this file output stream.FileChannel dstChannel = new FileOutputStream("dst.dat").getChannel(); // transfer bytes into this channel's file from the given readable byte channel dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); // close channels srcChannel.close(); dstChannel.close(); }catch (IOException e) {System.out.println("I/O Error: " + e.getMessage());}}}This was an example of how to copy files using FileChannel in Java.
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!
We will contact you soon.
Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor atJava Code Geeks.



