Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Wednesday, July 27, 2022

How to Copy a File in Java Program - Example Tutorial

How to copy files in Java from one directory to another is a common requirement, given that there is no direct method in File API for copying files from one location to another. A painful way of copying files is reading from FileInputStream and writing the same data to FileOutputStream to another directory. Though this process works 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. 

Thankfully you don't need to reinvent the wheel here, there are some open-source libraries available which allows us to copy file in Java easily from one directory to another.

One such library is Apache commons IO which contains a class called FileUtils, which provides a utility method for file-related operations. FileUtils.copyFile(sourceFile, targetFile) can be used to copy files in Java.  This Java program copy file from one location to other using FileUtils class.

This Java programming tutorial is next in series of earlier articles in File API like how to create a hidden files in Java and how to read from text files. If you are new to Java File API you may find them useful.

How to Copy a File in Java Program - Example Tutorial

Java Program to copy file in Java - Example Code:

copy file from one directory to other in java programBelow is the complete code example of copying one file in Java. We need to provide the absolute path of the source file to copy and destination directory. you can get the name of the file by calling File.getName() and FileUtils will create the same file in the destination directory with the same name.

import java.util.Date;

 * Simple Java program to copy files from one directory to another directory.
 * Java IO API doesn't provide any direct way to copy files but you can copy files
 * by copying its contents from InputStream to OutputStream. Though there are some
 * better ways to do it like Using Apache Commons Utils library has FileUtils class
 * to copy files in Java
 * @author Javin

public class FileCopyExample {

    public static void main(String args[]) throws IOException {
        //absolute path for source file to be copied
        String source = "C:/sample.txt";
        //directory where file will be copied
        String target ="C:/Test/";
        //name of source file
        File sourceFile = new File(source);
        String name = sourceFile.getName();
        File targetFile = new File(target+name);
        System.out.println("Copying file : " + sourceFile.getName() +" from Java Program");
        //copy file from one location to other
        FileUtils.copyFile(sourceFile, targetFile);
        System.out.println("copying of file from Java program is completed");
Copying file : sample.txt from Java Program
copying of file from Java program is completed

That's all on how to copy files in Java. This simple Java program can be extended to copy all files from one directory to another directory by just providing the name of the source and directory and then the Java program will pick up each file and create another file with the same name in the target directory. 

If you are going to do in plain Java its requires a lot of code and chances of error is high but if you use Apache Commons io library and FileUtils class it just matters of few line. As Joshua Bloch has rightly said in Effective Java, “prefer library over custom code”. Let me know if you find any bug on this Java File copy program.

Thank you for reading this Java File IO tutorial. If you have any doubt or questions then please ask in comments section. Happy to answer any doubt you may have.


Jirka Pinkas said...

Nice! Apache Commons rulez :) And it has even much more useful methods.

Anonymous said...

On Java 7, you don't need any 3rd party library, you can use java.nio.file.Files#copy(Path, Path, CopyOption...) instead.

Anonymous said...

This didnt work for me, cant see any copied file in the dest folder. The code runs fine though. Wierd??? any suggestion?

Anonymous said...

Outdated: Java8 supports file copy:

Anonymous said...

You can also use FileChannel to copy file from one location to another in Java. Its very efficient, espcially for copying large files.

As anonymous said, no need to use Apache Commons IO if you are running in Java 7, use the standard Files.copy() method for copying file from one folder to another. Its simple, easy and efficient.

Post a Comment