codescracker


java

Java Program to Copy Files



« Previous Program Next Program »


Copy Files

To copy the content of one file to the other file in Java programming, first, you have to ask to the user to enter the source and destination file name with extension to copy the contents of the source file to the destination file as shown in the following program.

Java Programming Code to Copy Files

Following are the two Java programs, the first program overwrites the content of the source file to the destination file whereas the second program append the content of the source file to the destination file.

Copy Files - Overwrite Content

Following Java program ask to the user to enter the two file name with extension i.e., source file name and the destination file name to copy (overwrite) the content of the source file on the destination file :

/* Java Program Example - Copy Files - Overwrite Content */
		
import java.io.*;
import java.util.Scanner;

public class JavaProgram {
 
    public static void main(String args[])
    {	
        String srcfname, dstfname;
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter Source File Name (with extension like source.txt) : ");
        srcfname = scan.nextLine();
        System.out.print("Enter Destination File Name (with extension like destination.txt) : ");
        dstfname = scan.nextLine();
        
    	InputStream inStream = null;
    	OutputStream outStream = null;
		
    	try
    	{
    	    File file1 =new File(srcfname);
    	    File file2 =new File(dstfname);
 
    	    inStream = new FileInputStream(file1);
    	    outStream = new FileOutputStream(file2);
 
    	    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("File Copied..");
    	}
    	catch(IOException e)
    	{
    		e.printStackTrace();
    	}
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program to copy file

Copy File - Append Content

Following Java program ask to the user to enter the two file name with extension (source file and destination file) to copy (append) the content of the source file on the destination file :

/* Java Program Example - Copy File - Append Content */
		
import java.io.*;
import java.util.Scanner;

public class JavaProgram {
 
    public static void main(String args[])
    {	
        String srcfname, dstfname;
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter Source File Name (with extension like source.txt) : ");
        srcfname = scan.nextLine();
        System.out.print("Enter Destination File Name (with extension like destination.txt) : ");
        dstfname = scan.nextLine();
        
    	InputStream inStream = null;
    	OutputStream outStream = null;
		
    	try
    	{
    	    File file1 =new File(srcfname);
    	    File file2 =new File(dstfname);
 
    	    inStream = new FileInputStream(file1);
    	    outStream = new FileOutputStream(file2, 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("File Copied..");
    	}
    	catch(IOException e)
    	{
    		e.printStackTrace();
    	}
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program to copy files content

After running the above Java Program, your target file will contain the source file's content

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test