import java.io.*; public class CopyFile { public static void main(String args[]) { // Check for two command line arguments if (args.length != 2) { System.out.println( "Improper arguments use:" + " java CopyFile infile outfile"); System.exit(-1); } // Get infile argument String infile = args[0]; // Get outfile argument String outfile = args[1]; // Create variables for in/out streams FileInputStream fis = null; FileOutputStream fos = null; try { // Create in stream fis = new FileInputStream(infile); // Create out stream fos = new FileOutputStream(outfile); // Declare variable for each byte read int ch; // Read byte til end of file while ((ch = fis.read()) != -1) { // Put bytes read into out stream fos.write(ch); } // Catch FileNotFoundException } catch (FileNotFoundException e) { System.err.println("File not found: " + e); // Catch IOException } catch (IOException e) { System.err.println("I/O problems: " + e); // Close files } finally { if (fis != null) { try { fis.close(); } catch (IOException ignored) { } } if (fos != null) { try { fos.close(); } catch (IOException ignored) { } } } } }