import java.io.*;

public class Copy {

  public static boolean validArgs(String[] args) {
     // Check number of arguments.
    if (args.length != 2) {
      System.err.println("Usage: java CopyFile sourceFile targetfile");
      return false;
    }

    // Check if source file exists
    if ( ! new File(args[0]).exists() ) {
      System.err.println("Source file " + args[0] + " not exist");
      return false;
    }

    // Check if target file exists
    if ( new File(args[1]).exists() ) {
      System.err.println("Target file " + args[1] + " already exists");
      return false;
    }

    // Otherwise, arguments are valid.
    return true;
  }

  public static void main(String[] args) throws IOException {
    // Check arguments.
       if ( ! validArgs(args) )
        return;

    // Create input and output File objects and streams.
    File sourceFile = new File(args[0]);
    BufferedInputStream input =
      new BufferedInputStream(new FileInputStream(sourceFile));
    System.out.println("The file " + sourceFile + " has "+
      input.available() + " bytes");

    File targetFile = new File(args[1]);
    BufferedOutputStream output =
      new BufferedOutputStream(new FileOutputStream(targetFile));
   
    // Continuously read a byte from input and write it to output
    // -1 returned for EOF
    int r;
    while ((r = input.read()) != -1)
      output.write(r); 

    input.close();
    output.close();

    System.out.println("Copy complete.");
  }
}
