Java Program to Copy the Content of One File to Another

This article is created to cover a program in Java that copies the content of one file to another. There are two ways, we can create the program:

  1. Copy a file - overwrite the content
  2. Copy a file - append the content

That is, the program creating using first way, copies the content of one (source) file to another (destination) file in a way that the previous content of destination file gets deleted.

And the program created using second way, copies the content of one file to another, without deleting the previous content.

Things to Do before Program

Since the program given below is used to copy the content of one file to another. That is, we're going to deal with two files. Therefore, that two files must be available inside the current directory to cross-check the program.

The current directory is the directory, where the Java source code to copy a file, is saved. Here is the snapshot of the current directory in my case. This snapshot also includes that two newly created file along with different contents.

java copy content of one file to another

Now let's copy the content of codes.txt file to cracker.txt file using the Java program given below.

Copy File in Java - Overwrite Content

The question is, write a Java program to copy the content of one file to another. The name of both files must be received by user at run-time of the program. The program given below is its answer. The content of source file gets copied to destination file.

import java.util.Scanner;
import java.io.*;

public class CodesCracker
{
   public static void main(String[] args)
   {
      String sourceFile, destFile, line, content="";
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the Name of Source File: ");
      sourceFile = scan.nextLine();
      try
      {
         FileReader fr = new FileReader(sourceFile);
         BufferedReader br = new BufferedReader(fr);
         
         for(line=br.readLine(); line!=null; line=br.readLine())
            content = content + line + "\n";
         
         br.close();
         
         System.out.print("Enter the Name of Destination File: ");
         destFile = scan.nextLine();
         
         try
         {
            FileWriter fw = new FileWriter(destFile);
            fw.write(content);
            fw.close();
            System.out.println("\nFile copied successfully!");
         }
         catch(IOException ioe)
         {
            System.out.println("\nSomething went wrong!");
            System.out.println("Exception: " +ioe);
         }
      }
      catch(IOException ioe)
      {
         System.out.println("\nSomething went wrong!");
         System.out.print("Exception: " +ioe);
      }
   }
}

The snapshot given below shows the sample run of above program Java program on copying the content of one file to another, with user input codes.txt as source, and cracker.txt as destination file.

java program to copy file

Now if you see the files, then the content of cracker.txt file holds the same content, of codes.txt file. Here is the new snapshot of the current directory along with these two files, after executing the above program:

copy file program in Java

Copy File in Java - Append Content

To copy the content of one file to another in a way that, the previous content does not gets deleted, then you need to change only one statement from the above program. That is, replace the following statement from above program:

FileWriter fw = new FileWriter(destFile);

with the statement given below:

FileWriter fw = new FileWriter(destFile, true);

That's it. Now if you execute the same program after changing this single statement, the content of source file gets copied to the destination file without deleting the previous content of destination file.

You'll get the same sample run as of previous program. That is, after copying the content of file, the program prints the message saying File copied successfully!. But after executing the program, this time, the content of two files will be, as shown in the new snapshot of the current directory, along with that two opened files:

java copy file program example

Note - To read the file, refer to Read & Display File's Content

The same program of copying the content of one file to another, can also be written as:

import java.util.Scanner;
import java.io.*;

public class CodesCracker
{
   public static void main(String[] args)
   {
      String sourceFile, destFile;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter Source File: ");
      sourceFile = scan.nextLine();
      System.out.print("Enter Destination File: ");
      destFile = scan.nextLine();
      
      InputStream inStream = null;
      OutputStream outStream = null;
      
      try
      {
         File FileSrc = new File(sourceFile);
         File FileDst = new File(destFile);
         
         inStream = new FileInputStream(FileSrc);
         outStream = new FileOutputStream(FileDst, true);
         
         byte[] buffer = new byte[1024];
         int length;
         
         while((length = inStream.read(buffer)) > 0)
            outStream.write(buffer, 0, length);
         
         if(inStream != null)
            inStream.close();
         if(outStream != null)
            outStream.close();
         
         System.out.println("\nFile Copied successfully!");
      }
      catch(IOException e)
      {
         System.out.println("\nError occurred!");
         System.out.println("Exception: " +e);
      }
   }
}

Same Program in Other Languages

Java Online Test


« Previous Program Next Program »



Like/Share Us on Facebook 😋