2007年11月8日 星期四

3

•import java.io.FileInputStream;
•import java.io.DataInputStream;
•import java.io.InputStreamReader;
•import java.io.BufferedReader;
•import java.io.IOException;
•import java.io.PrintStream;
•import java.io.FileOutputStream;
•class ComputeDistFileIO {
• public static void main (String args[]) {
• String thisLine;
• //Loop across the arguments
• for (int i=0; i < args.length; i++) {
• //Open the file for reading
• try {
• FileInputStream fin = new FileInputStream(args[i]);
• FileOutputStream fout = new FileOutputStream("outdata.txt");
• // now turn the FileInputStream into a DataInputStream
• try {
• // DataInputStream myInput = new DataInputStream(fin);
• BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
• PrintStream myOutput = new PrintStream(fout);
• try {
• while ((thisLine = myInput.readLine()) != null) {
• System.out.println(thisLine);
• myOutput.println(thisLine);
• } // while loop ends here
• }
• catch (Exception e) {
• System.err.println("Error: " + e);
• }
• } // end try
• catch (Exception e) {
• System.err.println("Error: " + e);
• }
• } // end try
• catch (Exception e) {
• System.err.println("failed to open file " + args[i]);
• System.err.println("Error: " + e);
• }
• } // for end here
• } // main ends here
•}