Different Ways to Copy Content From One File to Another File in Java

In Java, we can copy the contents of one file to another file. This can be done by the FileInputStream and FileOutputStream classes.

FileInputStream Class

It is a byte input stream class which helps in reading the bytes from a file. It provides different methods to read the data from a file.

FileInputStream fin = new FileInputStream(filename);

This creates a FileInputStream object fin given a filename to it from where it will copy the content or do read operation.



Methods used:

1. read(): This method is used to read a byte of data. It returns that byte as an integer value or return -1 if the end of file is reached.

2. close(): This method is used to close the FileInputStream.

FileOutputStream Class

It is a byte output stream class which helps in writing the bytes to a file. It provides different functions to write the data to a file.

FileOutputStream fout = new FileOutputStream(filename);

This creates a FileOutputStream object fout given a filename to which it will write the read content.

Methods used:

1. write(): This method is used to write a byte of data to the FileOutputStream.

2. close(): This method is used to close the FileInputStream.



File Class

File f = new File(filename);

This creates an File object named f, given a string object which is the file name or location path of that file which is to be accessed.

Note: If the filename given here does not exist, then the File class will create a new file of that name.  

Example:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to copy content from
// one file to another
  
import java.io.*;
import java.util.*;
  
public class CopyFromFileaToFileb {
    
    public static void copyContent(File a, File b)
        throws Exception
    {
        FileInputStream in = new FileInputStream(a);
        FileOutputStream out = new FileOutputStream(b);
  
        try {
  
            int n;
  
            // read() function to read the
            // byte of data
            while ((n = in.read()) != -1) {
                // write() function to write
                // the byte of data
                out.write(n);
            }
        }
        finally {
            if (in != null) {
  
                // close() function to close the
                // stream
                in.close();
            }
            // close() function to close
            // the stream
            if (out != null) {
                out.close();
            }
        }
        System.out.println("File Copied");
    }
  
    public static void main(String[] args) throws Exception
    {
        Scanner sc = new Scanner(System.in);
  
        // get the source file name
        System.out.println(
            "Enter the source filename from where you have to read/copy :");
        String a = sc.nextLine();
  
        // source file
        File x = new File(a);
  
        // get the destination file name
        System.out.println(
            "Enter the destination filename where you have to write/paste :");
        String b = sc.nextLine();
  
        // destination file
        File y = new File(b);
  
        // method called to copy the
        // contents from x to y
        copyContent(x, y);
    }
}

chevron_right


Output

Enter the source filename from where you have to read/copy :
sourcefile.txt
Enter the destination filename where you have to write/paste :
destinationfile.txt
File Copied

 Source File 

Source File

Destination File 

Destination File


Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.