Java program to copy file to another file using java.io package classes.

Below code is used to copy a file to another file using java.io package classes:

import java.io.*;

class Copyfile
{
    public static void main(String[] args)
    throws IOException
    {
        int i;
        FileInputStream fin;
        FileOutputStream fout;
        try
        {
            try
            {
                fin=new FileInputStream(args[0]);
            }
            catch(FileNotFoundException e)
            {
                System.out.println("Input file not found");
                return;
            }
            try
            {
                fout=new FileOutputStream(args[1]);
            }
            catch(FileNotFoundException e)
            {
                System.out.println("Errors opening output file");
                return;
            }
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println("Usage : Copy file from to");
            return;
        }
        try 
        {
            do
            {
                i=fin.read();
                    if(i!=-1)fout.write(i);
                }while(i!=-1);
            } 
        catch(IOException e)    
        {
            System.out.println("File error");
        }       
        fin.close();
        fout.close();
    }
}
Thank You.

© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT