UNB/ CS/ David Bremner/ teaching/ old/ cs1083/ java/ FileCopy4.java
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
public class FileCopy4{
    private static void usage(){
        System.out.println("Usage: java FileCopy4 infile outfile");
        System.exit(1);
    }
    public static void main(String [] args){
        if (args.length <2)  usage();
        BufferedInputStream in = null;
        try{
            in= new BufferedInputStream(
                        new FileInputStream(args[0]));
        } catch (FileNotFoundException e){
            System.out.println("bad input: "+args[0]);
            System.exit(1);
        }
        BufferedOutputStream out=null;
        try{
            out =new BufferedOutputStream(
                        new FileOutputStream(args[1]));
        } catch (FileNotFoundException e){
            System.out.println("bad output: "+args[1]);
            System.exit(1);
        }

        StopWatch clock=new StopWatch();
        clock.start();
        long bytes=0;
        try {
            int b=in.read();
            while (b>=0){
                bytes++;
                out.write(b);
                b=in.read();
            }
        } catch (IOException e){
            System.out.println(e.getMessage());
            System.exit(1);
        }
        clock.stop();
        System.out.println(bytes+" bytes copied in "+
                           clock.getElapsedTime()+"ms");
    }



}
//