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
// 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);    }} |
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
