Java Program for COPYFILE class

Here is a simple java program showing you how to use copyfile class…

import java.io.*;
class Copyfle
   {
        public static void main(String args[]) throws IOException
     {
    DataInputStream in=new DataInputStream(System.in);
    int i;
    FileInputStream fin=null;
    FileOutputStream fout=null;
    String filename=null;
    try
    {
    System.out.println(“Enter the filename to copy to another file”);
    filename=in.readLine();
    try
    {
    fin=new FileInputStream(filename);
    }
    catch (Exception e)
    {
    System.out.println(“file not found”);
    return;
    }
    try
    {
    System.out.println(“enter the filename to copy”);
    filename=in.readLine();
    fout=new FileOutputStream(filename);
    }
    catch (FileNotFoundException e)
    {}
    }
    catch(ArrayIndexOutOfBoundsException e)
    {}
    try
    {
    do
    {
    i=fin.read();
    if(i!=-1) fout.write(i);
    }while(i!=-1);
    System.out.println(“The file copied to ” + filename + “Successfully”);
    }
        catch(Exception e)
    {
    System.out.println(“file error”);
    }   
    fin.close();
    fout.close();
    }
}

   

Add Comment