Helping Information

Java Program to Copy Files

Java Program to Copy File

Problem Definition: Write Java Program to Copy File

In this tutorial, we will specifically learn how to create a Java Program to Copy Files.

On the positive side if you don’t have the latest version of java click here.

The Java File class doesn’t provide any method to copy one file to another. However, we can use Java I/O Streams to read content from one file and write to another.

We are significantly use 2 important class for this operation.

  1. FileInputStream
  2. FileOutputStream
Java Program to Copy Files
Copy Files

Example: Java Program to Copy Files using i/o streams.



class Main {

  public static void main(String[] args) {

    byte[] array = new byte[50];

    try {

      FileInputStream sourceFile = new FileInputStream("input.txt");

      FileOutputStream destFile = new FileOutputStream("newFile");

      // reads all data from input.txt;

      // writes all data to newFile


      System.out.println("The input.txt file is copied to newFile.");

      // closes the stream




    catch (Exception e) {




  1. Output:
The input.txt file is copied to newFile.

As shown above example, we have used the FileInputStream and FileOutputStream to copy one file to another.

Another key points,

FileInputStream reads all the content from input.txt to an array.
FileOutputStream writes all the content from the array to newFile.

The FileUtils class of package provides the copyFile() method to copy the file.
The Files class of java.nio package provides the copy() method to copy the file.

With this program, you will easily be transferring files to another file. This helps you with different extension files. But, the file should be text format. 

This program helps you when you want to copy multiple files. You can make this code as global. So you can accept the file as an input and it gives you immediate output. 

Related Topics:

  1. Java Program to List Files in a Directory
  2. Java Program to Rename File
  3. Java Program to Create Directories
  4. Java Program to Find ASCII Value of a character.

1 comment / Add your comment below

Leave a Reply

Your email address will not be published. Required fields are marked *