Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Io Java - Copying one file to another

Related Tutorials


 
 

Share on Google+Share on Google+

Java - Copying one file to another

Advertisement
This section provides an example that helps you for copying one file to another.

Java - Copying one file to another

     

Introduction

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

In this example we are using File class of java.io 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.

Explanation

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.


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

Code of the Program : 

import java.io.*;
 


public class CopyFile{

  private static void copyfile(String srFile, String dtFile){
 
  
try{
 
 
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 = in.read(buf)) 0){
  
  
out.write(buf, 0, len);
 
 
}
 
 
in.close();

  out.close();
 
 
System.out.println("File copied.");
 
  
}
 
 
catch(FileNotFoundException ex){
  
  
System.out.println(ex.getMessage() " in 
  the specified directory."
);
  
  
System.exit(0);

  }
 
 
catch(IOException e){
  
  
System.out.println(e.getMessage());  
  

  }

  }

  public static void main(String[] args){
 
 
switch(args.length){
 
  
case 0: System.out.println("File has not mentioned.");

    System.exit(0);
 
 
case 1: System.out.println("Destination file has not mentioned.");

    System.exit(0);

  case 2: copyfile(args[0],args[1]);
 
 
System.exit(0);

  default : System.out.println("Multiple files are not allow.");
 
 
System.exit(0);

  }

  }

}

Output of program:

Here the data of "Hello.java" file is copied to the Filterfile.txt file.

C:\nisha>javac CopyFile.java

C:\nisha>java CopyFile a.java Filterfile.txt
File copied.

C:\nisha>

You can even use this program for copying files from one hard drive to another hard drive.

Download File Copy Example

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 16, 2007

Related Tutorials

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
Ankur Yadav
April 3, 2012
java

it is good Example
saurabh
August 23, 2012
to say thnx

i think the overall outcomes should have some change to the body of executing program.....but not a mention ..thnx fr it...
abc
December 11, 2012
ksgms

this program is incorrect...
DMCA.com