/**
 * A simple program to copy files.
 *
 * @version    $Id: FileCopy.java,v 1.2 1999/12/13 03:39:39 jeh Exp $
 *
 * @author     Paul Tymann
 *
 * Revisions:
 *     $Log: FileCopy.java,v $
 *     Revision 1.2  1999/12/13 03:39:39  jeh
 *     style changes only
 *
 *     Revision 1.1  1999/12/13 01:50:21  jeh
 *     Initial revision
 *
 */

import java.io.*;

public class FileCopy {

    public static void main( String args[] ) {

        // Check command line arguments
        if ( args.length != 2 ) {
            System.out.println( "Usage: FileCopy sourceFile destFile" );
            System.exit(1);
        }

        // Open the input file.  A BufferedReader is used here to make
        // the copy more efficient.  The copy could have been done
        // using the FileInputStream only.
        //
        // The FileInputStream was used instead of a FileReader since
        // the program is interested in copying bytes and not necessarily
        // characters/strings; that is, the file may be binary.

        InputStream src = null;

        try {
            src = new BufferedInputStream( new FileInputStream( args[0] ) );
        }
        catch ( FileNotFoundException e ) {
            System.out.println( "FileCopy: " + e );
            System.exit(1);
        }

        // Take care of the output side.  If the output file exists, it
        // will be overwritten.

        OutputStream dest = null;

        try {
            dest = new BufferedOutputStream( new FileOutputStream( args[1] ) );
        }
        catch ( IOException e ) {
            System.out.println( "FileCopy: " + e );
            System.exit(1);
        }
//

        try {
            int d;

            // Copy the files a byte at a time

            d = src.read();
            while ( d != -1 ) {
                dest.write( d );
                d = src.read();
            }

            // Close the files.  Note since this is done inside
            // the same try block as the read, it is not easy to
            // determine if a read, write, or close threw the
            // exception.

            src.close();
            dest.close();
        }
        catch ( IOException e ) {
            System.out.println( "FileCopy:  File I/O Error" );
            // I'd like to close the streams here to be safe, but they
            // could raise an IOException, so I'm stuck.
            System.exit(1);
        }
    }
} // FileCopy

