Program to copy contents of one file into other file

This program copied the contents of one file into other file suing FileInputStream and FileOutputStream classes. The name of files for reading and writing are passed through command line arguments.
PROGRAM
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

class CopyFileContentsInOtherFile {

 public static void main(String[] args) {

  try
  {
   File f1=new File(args[0]);
   File f2=new File(args[1]);

   if(f1.exists()==false)
   {
    System.out.println("Can't Copy Source File Not Exists");
    System.exit(0);
   }
   if(f2.exists()==true)
   {
    System.out.println("Can't Copy Destination File Exists");
    System.exit(0);
   }
 
   FileInputStream fis=new FileInputStream(f1);
   FileOutputStream fos=new FileOutputStream(f2);
 
   int x;
   while((x=fis.read())!=-1) {
    fos.write(x);
   }
   
   System.out.println("Contents of file '"+f1+"' are copied in '"+f2+"' successful...");
   fis.close();
   fos.close();
  }
  catch(Exception ee) { 
   System.out.println(ee);
  }
 }

}
OUTPUT
C:\>javac CopyFileContentsInOtherFile.java
C:\>java CopyFileContentsInOtherFile   abc.txt   xyz.txt
Contents of file 'abc.txt' are copied in 'xyz.txt' successful...

Comments

Popular posts from this blog

Develop an Android application to display “Hello World!” on screen

Program to design an applet which draws a circle (having color BLUE) inside a triangle (having color YELLOW)

Build a Simple Android App to Display Student Details