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...

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative