Java

Java Copy File with 5 different ways


Before JDK 7 there is didn’t any readymade function to copy file quickly, and you have to create manually file operation like you convert a file into bytes stream using FileInputStream, and then you write those bytes stream into another file. Another way is to use NIO (New I/O) package FileChannel technique is usually faster than essential streams because of Buffer-oriented. After JDK 7 File.copy method include in JDK and no need to use third party library have but somehow there is own pros and cons. Here there are four different ways to java copy file with examples that you need to know.

Java Copy File

1. Using FileInputStream

Traditional approach FileInputStream as above I mention using java IO streaming API. It is slower then other methods because Blocking IO and fully stream oriented. I also analyzed when you size of Buffer increase copy file time duration is decrease in below code I used 1024.

private void copyFileUsingIO(File sourceFile, File destinationFile) throws IOException {
	InputStream inputStreamData = null;
	OutputStream outputStreamData = null;

	try {
		inputStreamData = new BufferedInputStream(new FileInputStream(sourceFile));
		outputStreamData = new BufferedOutputStream(new FileOutputStream(destinationFile));
		byte[] buffer = new byte[1024];
		int length;
		while ((length = inputStreamData.read(buffer)) > 0) {
			outputStreamData.write(buffer, 0, length);
		}
			
	} finally {
		inputStreamData.close();
		outputStreamData.close();
	}
}

2. Using FileChannel

FileChannel is introducing in JDK 4 on NIO (New IO) with a significant benefit of Non-blocking I/O based on Buffer. Don’t use when files size is larger because of buffer if you have sufficient IO then it’s work like charm.


It’s won’t is slower than buffering yourself from one channel to the another, and will probably be massively accelerated. According to the Javadocs:

Many operating systems can transfer bytes directly from the filesystem cache to the target channel without actually copying them.

Here is example how to copy file using FileChannel

private void copyFileUsingNIO(File sourceFile, File destinationFile) throws IOException {
		FileInputStream inputStream = new FileInputStream(sourceFile);
		FileOutputStream outputStream = new FileOutputStream(destinationFile);
		FileChannel inChannel = inputStream.getChannel();
		FileChannel outChannel = outputStream.getChannel();
		try {	
			inChannel.transferTo(0, inChannel.size(), outChannel);
		} finally {
			inChannel.close();
			outChannel.close();
			inputStream.close();
			outputStream.close();
		}
	}

3. Using Files Class

 Files.copy(File from, File to)  convenient method for us when you use Java 7 or upgraded version.

private void copyFileUsingJava7Files(File sourceFile, File destinationFile) throws IOException {
    Files.copy(sourceFile.toPath(), destinationFile.toPath());
}

4. Using Google Guava

One more API for copy file using java when you use google guava in your application. It has one simple method  copy(File from, File to)  make it easy and more code readability. Guava internal use Java NIO FileChannel.

private void copyFileUsingGuava(File sourceFile, File destinationFile) throws IOException{
    Files.copy(sourceFile,destinationFile);          
}

5. Using Apache Commons IO FileUtils

Apache Commons is one of most used package generally in all project we include it.  Apache Commons IO has one method FileUtils.copyFile(File srcFile, File destFile)  easily copy a file as like above we discuss Guava library.  FileUtils.copyFile  method internally uses Java NIO FileChannel as like Guava.

private void copyFileUsingApacheCommonsIO(File sourceFile, File destinationFile) throws IOException {
    FileUtils.copyFile(sourceFile,destinationFile);
}

Reference

  1. FileChannel transferTo() from JavaDoc
  2. copy method from Google Guava Common IO Doc
  3. FileUtils.copyFile method from Apache Commons Doc
Java
4 ways to Convert String to int in Java
  • Holger

    HolgerHolger

    Author Reply

    Please improve your English or have it proofread by a proficient English speaker. Technically, your article is great.