// This program will read in a file from the command line // and write it out to the screen. // To run program from the command line type: java Rwtest testfile.txt // Distributed under the GNU GPL: www.gnu.org // import the input/output package import java.io.*; public class Rwtest { public static void main(String args[]) throws IOException { if (args.length == 0) { System.out.println("Please enter a file name on the command line"); System.exit(0); } // Take the file at position 0 in the 'args' array that was typed in on the // command line and place it into File object 'alpha' File alpha = new File(args[0]); // Pass File object 'alpha' into FileReader object 'bravo' FileReader bravo = new FileReader(alpha); // Pass FileReader object 'bravo' into BufferedReader object // 'charlie' to make it more efficient BufferedReader charlie = new BufferedReader(bravo); // Create new file to write to called Writetest.txt // and place it in object kilo File kilo = new File("Writetest.txt"); // Pass object kilo into FileWriter golf FileWriter golf = new FileWriter(kilo); // Pass object golf into BufferedWriter hotel to make it more // efficient in writing BufferedWriter hotel = new BufferedWriter(golf); // Pass object hotel into PrintWriter object juliet PrintWriter juliet = new PrintWriter(hotel); int delta; // Read each character from the file in BufferedReader 'charlie' // and place it into String variable 'delta' // continue doing this until there is no text left // ie: reads in a -1 value while((delta=charlie.read())!=-1) { // Print out the integer read into 'delta' to the screen System.out.println("The integer just read in is: " + delta); // Write integer to object 'juliet' which eventually // writes it to 'Writetest.txt' file juliet.write(delta); // Write integer to screen again after having written // it to file System.out.println("Writing " + delta + " to file."); } // end while // close the file in BufferedReader object 'charlie' charlie.close(); // close the PrintWriter object 'juliet' // that was just written to hard drive juliet.close(); } // end main method } // end class Rwtest