Home » Core Java » nio » FileChannel » Copying binary file with FileChannel

About Byron Kiourtzoglou

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 at Java Code Geeks.

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 a FileChannel so as to be able to read data from. To do so you can create a FileInputStream 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 a FileChannel so as to be able to write data to. To do so you can create a FileOutputStream 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.

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.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

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 ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of