// File: Copy.java
// Author: Mike Wade
// Last Modified: 12/2/1999

import java.io.*;


/**
 * This program is used to copy files.  The user is prompted to name
 * the source file and the destination file.  If the destination file
 * already exists, the user is asked to confirm over-writing the old
 * file.
 **/
class Copy {

    
    /**
     * Used for reading from the keyboard
     **/
    private static BufferedReader stdin = new BufferedReader(
        new InputStreamReader(System.in));


    /**
     * Reads and returns a String from stdin, using the specified prompt
     **/
    private static String getLine(String prompt) throws IOException {
        System.out.print(prompt);
        return stdin.readLine();
    }


    public static void main(String [] args) throws IOException {
        // Get the name of the source and destination files
        String sourceName = getLine("Enter the name of the source file: ");
        String destName = getLine("Enter the name of the destination file: ");
        
        // open the specified files
        File sourceFile = new File(sourceName);
        File destFile = new File(destName);

        // If the destination file is a directory, assume that the file is to 
        // be copied to this new directory (No, we never talked about this 
        // alternate constructor, but your book does (page 494)
        if (destFile.isDirectory()) {
            destFile = new File(destName, sourceName);
            // update the dest name for the error checking below
            destName = "File " + sourceFile + " in directory " + destName;
        }

        // If the source file does not exist, we exit the program
        if (sourceFile.exists() == false) {
            System.err.println("Copy: cannot access " + sourceName);
            System.exit(1);
        }

        // If the source file is a directory, we exit the program
        if (sourceFile.isDirectory()) {
            System.err.println("Copy: " + sourceName + " is a directory");
            System.exit(1);
        }

        // If the destination file already exists, confirm that the file
        // should be removed
        if (destFile.exists()) {
            // make sure that
            String response = getLine("Copy: overwrite " + 
                                      destName + " (y/n)? ");
            // assume that if the user entered something other than "y", the
            // file should not be overwritten; so exit
            if (response.equals("y") == false) {
                System.exit(0);
            }
        }

        // Object to read the file
        BufferedReader fin = new BufferedReader(new FileReader(sourceFile));

        // Object to write the new file
        PrintWriter fout = new PrintWriter(new FileWriter(destFile));

        // Read the source file one line at a time, writing each line to
        // the destination file
        while (fin.ready()) {
            fout.println(fin.readLine());
        }


        fout.close();
        fin.close();
    }
}
