Copying byte from one file to another file.

import java.io.*;
public class FileDemo
{
    public static void main(String args[]) throws IOException
    {
        if(args.length != 2)
        {
            System.out.println("\nFile Not Found.....!!!");
            System.exit(0);
        }
   
  File f=new File(args[0]);
        byte b[]={};
        //Reading File...
        if(f.exists())
        {
            FileInputStream f1=new FileInputStream(f);
            int num=f1.available();
            b=new byte[num];
            int n=f1.read(b);
            String str=new String(b);
            System.out.println("\nContent :"+str);
            f1.close();
            f=null;
        }
        else
        {
            System.out.print("\nFile Does not Exist");
            System.exit(0);
        }
       
        //Writing To File
       
        File f3=new File(args[1]);
        if(!f3.exists())
            System.out.println("\n"+args[1] +" is New File");
        else
            System.out.println("\n"+args[1]+ " is exists,will be overwritten.");
        System.out.print("\nOpening file "+args[1]);
        FileOutputStream f2=new FileOutputStream(f3);
        System.out.println("\nFile Opened,Now Writting Contents "+args[1]);
        f2.write(b);
        f2.flush();
        System.out.println("\nContent Written..");
        System.out.println("\nFile Closing..");
        f2.close();
    }
   
}

No comments: