Java

How to copy files in Java from one directory to another is a basic requirement, as stated there is no direct method to File API for copying files from one location to another. An irritating way to copying files is reading from FileInputStream and writing the same data to FileOutputStream to another directory. It’s pretty raw to work with and the best approach is for anyone to create a library for common File operations like cut, copy, paste, etc.

How to use JDK7 FileChannel.transferTo() method to copy files and directories in Java, as well as FileUtils from Apache Commons.

Hello folks. I have been a Java Programmer for a long time, and you won’t believe it, but there was no file copying method in the Java API until Java 7. Our options were limited to: write it ourselves using a FileInputStream, a FileOutputStream, and a buffer to copy bytes from one to the other; or, better yet, use the FileChannel.transferTo() method or the Apache Commons FileUtils, which was a lifesaver in those days and still is today.

JDK has evolved alot by the time, and you have a decent API to copy files from one directory to another. In this article, I’ll show you both the pre-Java code for copying files from one directory to another as well post Java 7 code, which makes this task a lot easier.

Copying Files From One Directory To Another Before Java 7

Dependency: Apache Commons IO

FileUtils.copyFile (File Source, File Destination)

It copies a file to a new location preserving file timestamp. It also copies the code of the specified source file to the specified file directory. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

import java.io.File;

import java.io.IOException;



import org.apache.commons.io.FileUtils;



/**

* Java program to copy a file from one directory to another e.g. from src to dest

*

* @author Javin

*/

public class FileCopyDemo {



public static void main(String args[]) {



// Using Apache Commons FileUtils class

File srcFile = new File("bin/HelloWorld.class");

File destFile = new File("target/HelloWorld.class");

try {

FileUtils.copyFile(srcFile, destFile);

System.out.println("File successfully copied in Java");

} catch (IOException e) {

e.printStackTrace();

}



}



}

Output:

System.out.println("File successfully copied in Java");

Copies a File to a Directory Preserving the Timestamp

It copies the code of the specified source file to a file of the same name in the specified file directory. The destination directory file is created if it does not exist. If the destination file exists, then this method will overwrite it again.

import java.io.File;

import java.io.IOException;



import org.apache.commons.io.FileUtils;



/**

* Java program to copy a file from one directory to another like from src to dest

*

* @author Javin Paul

*/

public class Testing {



public static void main(String args[]) {



// Using Apache Commons FileUtils class

File srcFile = new File("bin/HelloWorld.class");

File destDir = new File("target");

try {

FileUtils.copyFileToDirectory(srcFile, destDir);



System.out.println("File successfully copied to destination directory in Java");

} catch (IOException e) {

e.printStackTrace();

}



}



}



Output

File successfully copied to destination directory in Java

Copying Files From One Directory To Another Using Java 7 NIO 2 API

In Java 7, there is a standard method to copy files: Files.copy.

It integrates with O/S native I/O for high performance.

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Path;



import static java.nio.file.StandardCopyOption.*;

import static java.nio.file.LinkOption.*;



/**

* Java program to copy file using Java 7 Files.copy() method

*

* @author Javin Paul

*/

public class FileCopyDemo {



public static void main(String args[]) {



try {

Path bytes = Files.copy(

new Java.io.File("bin/HelloWorld.class").toPath(),

new java.io.File("target/HelloWorld.class").toPath(),

REPLACE_EXISTING,

COPY_ATTRIBUTES,

NOFOLLOW_LINKS);

System.out.println("File successfully copied using Java 7 way");



} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}



}



}

You can also copy files in Java by writing code using FileInputStream and FileOuputStream, but its not required, given you have Java 7 installed.

Alternatively, Apache Commons IO FileUtils class is also handy way for copying. To speed up the process of file copy and transfer, you can also take an advantage of the java.nio and FileChannel class, but beware that there is a bug in Windows that prevents you from transferring more than 64GB of channel data.

Thanks for reading this article. If you like it, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.