Different Ways to Copy Files in Java – GeeksforGeeks

There are mainly 3 ways to copy files using java language. They are as given below:

  1. Using File Stream (Naive method)
  2. Using FileChannel Class
  3. Using Files class.

Note: There are many other methods like Apache Commons IO FileUtils but we are solely discussing copying files using java classes.

Method 1: Using File Stream (Naive method)

This is a naive method where we are using a file input stream to get input characters from the first file and a file output stream to write output characters to another file. This is just like seeing one file and writing onto another.

Example:

Tóm Tắt

Java




 

import java.io.*;

 

public class GFG {

 

    

    public static void main(String[] args)

        throws IOException

    {

 

        

        

        FileInputStream fis = null;

        FileOutputStream fos = null;

 

        

        try {

 

            

            

 

            

            fis = new FileInputStream(

                "C:\\Users\\Dipak\\Desktop\\input.txt");

 

            

            fos = new FileOutputStream(

                "C:\\Users\\Dipak\\Desktop\\output.txt");

 

            int c;

 

            

            

            

            while ((c = fis.read()) != -1) {

 

                

                

                fos.write(c);

            }

 

            

 

            

            System.out.println(

                "copied the file successfully");

        }

 

        

        

        

        finally {

 

            

 

            if (fis != null) {

 

                

                fis.close();

            }

            if (fos != null) {

 

                

                fos.close();

            }

        }

    }

}



 
Output:  

copied the file successfully 

 

For the above program, we require one input.txt and one output.txt file. Initially, both the text files look like this
 

 

After successful execution of the program,
 

 

Method 2: Using FileChannel Class 

This is a class present in java.nio, channels package and is used to write, modify, read files. The objects of this class create a seekable file channel through which all these activities are performed. This class basically provides two methods named as follows:

  • transferFrom(ReadableByteChannel src, long position, long count): Transfers bytes to the channel which calls this method from the src channel. This is called by destination channel. The position is the place of a pointer from where the copy actions are to be started. Count specifies the size of the file which is nearly equal to the amount of content it contains.
  • transferTo(long position, long count, WritableByteChannel target): Transfers bytes from the source or method calling channel to the destination channel of the file. This method is mainly called using the source channel and Count mentions the size of the source file and position from where the copy is to be made

 

Hence, we can use any one of the two methods to transfer files data and copy them. 

Example:

Java




 

import java.io.*;

import java.nio.channels.FileChannel;

 

public class GFG {

 

    

    public static void main(String[] args)

        throws IOException

    {

 

        

        

        FileChannel src

            = new FileInputStream(

                  "C:\\Users\\Dipak\\Desktop\\input.txt")

                  .getChannel();

        FileChannel dest

            = new FileOutputStream(

                  "C:\\Users\\Dipak\\Desktop\\output.txt")

                  .getChannel();

 

        

        try {

 

            

            

            dest.transferFrom(src, 0, src.size());

            

            

        }

 

        

        

        finally {

 

            

            

 

            

            src.close();

 

            

            dest.close();

        }

    }

}



Output: 

For the above program, we require one input.txt and one output.txt file. Initially, both the text files look like this

 

 

After successful execution of the program,

 

 

Method 3: Using Files Class 

This is a class present in java.nio.File package. This class provides 3 methods to copy the files which are as follows:
 

  • copy(InputStream in, Path target): Copies all bytes of data from the input file stream to the output path of the output file. It cannot be used to make a copy of a specified part in a source file. Here we are not required to create an output file. It is automatically created during the execution of the code.
  • copy(Path source, OutputStream out): Copies all bytes from the file specified in the path source to the output stream of the output file.
  • copy(Path source, Path target): Copies files using the path of both source and destination files. No need to create the output file here also.

 

Example:

Java




import java.nio.file.Files;

import java.io.*;

public class GFG{

   

    

    public static void main(String[] args) throws IOException{

       

        

        

        File src = new File("C:\\Users\\Dipak\\Desktop\\input.txt");

        File dest = new File("C:\\Users\\Dipak\\Desktop\\output.txt");

             

        

        Files.copy(src.toPath(), dest.toPath());

       

        

        

        

             

    }

}



Output:

For the above program, we require one input.txt and one output.txt file. Initially, both the text files look like this

After successful execution of the program,

Note: Out of all these methods the stream one is fast in process but if someone wants to be technical and more advanced than they can opt for the other two methods. Also the FileChannel method provides us a lot of options to control the part of the file to be copied and to specify its size.

My Personal Notes

arrow_drop_up