|
Copy1 |
|
1 // joi/9/copy/Copy1.java 2 // 3 // 4 // Copyright 2003 Bill Campbell and Ethan Bolker 5 6 import java.io.*; 7 8 /** 9 * Simple read-a-char, write-a-char loop to exercise file I/O. 10 * 11 * Usage: java Copy1 inputfile outputfile 12 */ 13 14 public class Copy1 15 { 16 private static final int EOF = -1; // end of file character rep. 17 18 /** 19 * All work is done here. 20 * 21 * @param args names of the input file and output file. 22 */ 23 24 public static void main( String[] args ) 25 { 26 FileReader inStream = null; 27 FileWriter outStream = null; 28 int ch; 29 30 try { 31 // open the files 32 inStream = new FileReader( args[0] ); 33 outStream = new FileWriter( args[1] ); 34 35 // copy 36 while ((ch = inStream.read()) != EOF) { 37 outStream.write( ch ); 38 } 39 } 40 catch (IndexOutOfBoundsException e) { 41 System.err.println( 42 "usage: java Copy1 sourcefile targetfile" ); 43 } 44 catch (FileNotFoundException e) { 45 System.err.println( e ); // rely on e's toString() 46 } 47 catch (IOException e) { 48 System.err.println( e ); 49 } 50 finally { // close the files 51 try { 52 if (inStream != null) { 53 inStream.close(); 54 } 55 } 56 catch (Exception e) { 57 System.err.println("Unable to close input stream."); 58 } 59 try { 60 if (outStream != null) { 61 outStream.close(); 62 } 63 } 64 catch (Exception e) { 65 System.err.println("Unable to close output stream."); 66 } 67 } 68 } 69 } 70
|
Copy1 |
|