import java.io.*;
// The TextReader class must also be available for this progrma to compile)

public class ReverseFileJAA {
    public static void main(String [] args) {
	TextReader data; //Character input stream for reading data.
	PrintWriter result; //Character output stream for writing data.
	
	// An array to hold all the numbers that are read from the file.
	double[] number = new double[1000]; 

	int numberCt = 0; // The number of items actually stored in the array.
	String inFileName = "data.dat";
	String outFileName = "result.dat";

	try {
	    // create an input stream
	    data = new TextReader( new FileReader( inFileName ));
	}
	catch (FileNotFoundException e){
	    System.err.println("Can't find file " + inFileName + "!");
	    return;
	} // end of catch block

	try {
	    //create an output stream
	    result = new PrintWriter( new FileWriter( outFileName ));
	} 
	catch (FileNotFoundException e){
	    System.err.println("Can't open file " + inFileName + "!");
	    System.err.println( e.toString() );
	    // Since we can not open the output file we must close the input file
	    data.close();
	    return;
	} // end of catch block 

	try {
	    //Read data from the input file.
	    while ( !data.eof() ) {
		// continue reading from the file until reach EOF.
		number[numberCt] = data.getlnDouble();
		numberCt++;
	    } // end of while loop

	    // Wrtie data to the output file

	    // Why can we not use number.length in the for loop below?
	    for ( int i = 0; i < numberCt; i++ ) {
		result.println( number[i] );
	    }

	    System.out.println( "Done!" );
	} // end of try block
	catch (TextReader.Error e) {
	    // There was a problem reading data from the file

	    // Why not use System.out.println in the statement below?
	    System.err.println( "Input Error: " + e.getMessage() );
	} // end of catch block
	catch (IndexOutOfBoundsException e) {
	    // Tried to go beyond the bounds of the array.
	    System.err.println( "Array index out of bounds exception while" +
				" reading from the input file.");
	} // End of catch block
	
	// Why are there no catch blocks for the output of information?

	finally {
	    // Close all files before exiting the program.
	    data.close();
	    result.close();
	} // end of finally block
    } // end of main method

} // end of class ReverseFileJAA
	    
	    

