Java - Copying one file to another

In this section, you will learn how to copy contents from one file to another file.

Java - Copying one file to another


This example illustrates how to copy contents from one file to another file. This topic is related to the I/O (input/output) of package.

In this example we are using File class of package. The File class is an abstract representation of file and directory pathnames. This class is an abstract, system-independent view of hierarchical pathnames. An abstract pathname has two components:

  1. An optional system-dependent prefix string,
    such as a disk-drive specifier, "/" for the UNIX root directory, or "\\" for a Win32 UNC pathname, and
  2. A sequence of zero or more string names.


This program copies one file to another file. We will be declaring a function called copyfile which copies the contents from one specified file to another specified file.

copyfile(String srFile, String dtFile)

The function copyfile(String srFile, String dtFile) takes both file name as parameter. The function creates a new File instance for the file name passed as parameter

File f1 = new File(srFile);
File f2 = new File(dtFile); 

and creates another InputStream instance for the input object and OutputStream instance for the output object passed as parameter

InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2); 

and then create a byte type buffer for buffering the contents of one file and write to another specified file from the first one specified file.

// For creating a byte type buffer
byte[] buf = new byte[1024];
// For writing to another specified file from buffer buf
out.write(buf, 0, len);

Code of the Program : 


public class CopyFile{
  private static void copyfile(String srFile, String dtFile){
  File f1 =
 new File(srFile);
  File f2 = new File(dtFile);
  InputStream in = new FileInputStream(f1);
  //For Append the file.
//  OutputStream out = new FileOutputStream(f2,true);

  //For Overwrite the file.
  OutputStream out = new FileOutputStream(f2);

 byte[] buf = new byte[1024];
  int len;
  while ((len = > 0){
  out.write(buf, 0, len);
"File copied.");
  catch(FileNotFoundException ex){
  System.out.println(ex.getMessage() + " in the specified directory.");
  catch(IOException e){
  public static void main(String[] args){
  case 0: System.out.println("File has not mentioned.");
  case 1: System.out.println("Destination file has not mentioned.");
  case 2: copyfile(args[0],args[1]);
  default : System.out.println("Multiple files are not allow.");

Download File Copy Example

Share on Google+Share on Google+

Java - Copying one file to another

Posted on: June 4, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java - Copying one file to another   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
October 15, 2011
java quesry

plz give solution of 1. Write File I/O programs for following operations : i) Copy a file ii) Replace a word with another word in a file iii) Working as linux ‘cat’ command iv) List files and directories within a file. v) Append within a file. vi) Count number of words within a file using console i/o 2. Define a layout manager in java, and explain its type and working in detail using a program. 3. Write a program to demonstrate generics in java using generic class, generic interface and its implementation class, generic constructor and generic methods. 4. Demonstrate multithreading using any sample application. 5. Implement a tcp client server program.
Dave Pierce
November 13, 2011
Trouble with CopyFile example

Thank you for all the example java programs. Most have been quite helpful. I am having trouble, however, with the CopyFile example. As written, it always defaults to args.length = 0, therefore not very useful. How is this CopyFile class used in a real example with source and destination filenames listed?
January 23, 2013

thanks alot, its working with .doc file
January 3, 2012
how to run above program

how to run above program
February 9, 2012
java related

hey post all commands pdf in your web site. how to do that commands
February 23, 2012
file and content names only

Hi Does any one know if there is a code to where you can copy a file name save it as a name only and then copy the content of the same file and save only the names of that for example .exe, jpeg, .dvx ect.. My aim is to copy only the so that a dont sit with dubble files on the pc. If there is any awnsers please email me at [email protected] Thank you
Ankur Yadav
April 3, 2012

no comments
Not Interested
October 1, 2012

Your Simple and to the point examples always make me to visit your website at first place whenever I needs some help related to Java.
April 21, 2015

Can this copying a file to another is possible through JSP. If so Please share your Codes..