How to copy files in Java? Example
Tags: java, programming, coding, beginners
Published: 2021-10-13 09:21:04

---
title: How to copy files in Java? Example
published: true
description: Multiple ways to copy files from one directory to another in Java with examples. 
tags: java, programming, coding, beginner
//cover_image: https://direct_url_to_image.jpg
---
Hello folks, I have been programming in Java for a long time and you won't believe but there was **no file copy method in the Java API until Java 7**. At that time, our options were either write it ourself, using a FileInputStream, a FileOutputStream and a buffer to copy bytes from one to the other - or better yet, use the [**FileChannel.transferTo() **](https://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html)method or just use the Apache Commons' FileUtils which was a life save those days and even today. 

JDK has evolved now 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.

* * * * *

## 1\. Copying File from Directory to other before Java 7
Dependency: Apache Commons IO

### 1\. FileUtils.copyFile(File source, File destination)

This method copies a file to a new location preserving file timestamp

This method copies the contents of the specified source file to the specified destination file. The directory holding the destination [file is created if it does not exis](https://www.java67.com/2014/02/how-to-create-file-and-directory-in-java.html)t. If the destination file exists, then this method will overwrite it.



```java
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");
```

### 1.2 Copies a file to a directory preserving the timestamp

This method copies the contents of the specified source file to a file of the same name in the specified destination directory. The destination [directory is created if it does not exis](https://javarevisited.blogspot.com/2011/12/create-file-directory-java-example.html)t. If the destination file exists, then this method will overwrite it.



```java
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
```

[![](https://dz2cdn1.dzone.com/storage/temp/15253331-how-to-copy-file-from-one-directory-to-other-in-ja.png)](https://javarevisited.blogspot.com/2016/02/how-to-copy-non-empty-directory-in-java.html)

### 2\. Copying Files from One Directory to Another using Java 7 NIO 2 API
In [Java 7](https://javarevisited.blogspot.com/2014/04/10-jdk-7-features-to-revisit-before-you.html#google_vignette), there is a standard method to copy files in java:

### Files.copy.

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


```java
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 blocke.printStackTrace();
        }
    }
}
```

You can also copy files in Java by writing code using [FileInputStream ](https://javarevisited.blogspot.com/2014/04/difference-between-fileinputstream-and-filereader-in-java.html)and [FileOuputStream](https://javarevisited.blogspot.com/2012/12/how-to-append-text-into-file-in-java-filewriter-example.html), but that's just not required, given you have Java 7 installed.

Alternatively Apache commons IO FileUtils class is also handy. For high-speed file copy and transfer, you can also take advantage of the java.nio and [FileChannel ](https://javarevisited.blogspot.com/2016/01/reading-writing-files-using-filechannel-bytebuffer-example.html#axzz6g8q23kRW)class but beware that there is a bug in Windows, which prevents you to transfer more than 64GB of channel data. Windows bug around the transferFrom not able to copy streams larger than 64MB in one piece?

Other **Java Programming Articles** you May like:
Other **Java and Programming articles** you may like to read.

-   [The Java Developer RoadMap](https://javarevisited.blogspot.com/2019/10/the-java-developer-roadmap.html)
-   [5 Free Spring  Framework Courses for Java Developers](http://www.java67.com/2017/11/top-5-free-core-spring-mvc-courses-learn-online.html)
-   [Top 5 Courses to Learn Microservices with Spring Boot](https://javarevisited.blogspot.com/2018/02/top-5-spring-microservices-courses-with-spring-boot-and-spring-cloud.html#axzz6JJFPbsyP)
-   [10 Courses to learn RESTful Web Service in Java](https://medium.com/javarevisited/top-5-books-and-courses-to-learn-restful-web-services-in-java-using-spring-mvc-and-spring-boot-79ec4b351d12)
-   [Top 5 Courses to learn Spring in Depth](https://javarevisited.blogspot.com/2018/06/top-6-spring-framework-online-courses-Java-programmers.html)
-   [5 Essential Frameworks Java Developer should learn ](https://medium.com/javarevisited/5-essential-frameworks-every-java-developer-should-learn-6ed83315f1fb)
-   [5 Online Courses to learn Core Java for Free](http://javarevisited.blogspot.sg/2017/11/top-5-free-java-courses-for-beginners.html#axzz4zuIICRs9)
-   [Top 5 Java design patterns courses for experienced Java devs](http://javarevisited.blogspot.sg/2018/02/top-5-java-design-pattern-courses-for-developers.html)
-   [Top 7 Courses to learn Microservices in Java](https://medium.com/javarevisited/top-5-courses-to-learn-microservices-in-java-and-spring-framework-e9fed1ba804d)
-   [10 Courses to learn Spring Security and OAuth2](https://medium.com/javarevisited/top-10-courses-to-learn-spring-security-and-oauth2-with-spring-boot-for-java-developers-8f0222d6066d)
-   [10 courses for Programming/Coding Job Interviews](http://javarevisited.blogspot.sg/2018/02/10-courses-to-prepare-for-programming-job-interviews.html)
-   [10 Advanced Spring Boot Courses for Java developers](https://medium.com/javarevisited/10-advanced-spring-boot-courses-for-experienced-java-developers-5e57606816bd)
-   [Top 5 Courses to learn Spring Cloud for Beginners](https://javarevisited.blogspot.com/2018/04/top-5-spring-cloud-courses-for-java.html)
-   [5 Courses to learn Shell Scripting in Linux](http://javarevisited.blogspot.sg/2018/02/5-courses-to-learn-shell-scripting-in-linux.html)
-   [10 Free Spring Boot Tutorials and Courses for Java Devs](https://medium.com/javarevisited/10-free-spring-boot-tutorials-and-courses-for-java-developers-53dfe084587e)

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

**P. S. -** If you are new to the Java world and looking for free courses to learn Java from scratch then you can also take a look at **[my favorite free Java courses](https://medium.com/javarevisited/10-free-courses-to-learn-java-in-2019-22d1f33a3915)** on Medium. It has some of the best free Java training courses to learn Java online.