import java.io.*; import java.util.ArrayList; import java.util.Scanner; /** * Reads numbers from a file named data.dat and writes them to a file * named result.dat in reverse order. The input file should contain * only real numbers. */ public class ReverseFileWithScanner { public static void main(String[] args) { Scanner data; // For reading the data. PrintWriter result; // Character output stream for writing data. ArrayList numbers; // An ArrayList for holding the data. numbers = new ArrayList(); try { // Create the input stream. data = new Scanner(new File("data.dat")); } catch (FileNotFoundException e) { System.out.println("Can't find file data.dat!"); return; // End the program by returning from main(). } try { // Create the output stream. result = new PrintWriter("result.dat"); } catch (FileNotFoundException e) { System.out.println("Can't open file result.dat!"); System.out.println("Error: " + e); data.close(); // Close the input file. return; // End the program. } while ( data.hasNextDouble() ) { // Read until end-of-file. double inputNumber = data.nextDouble(); numbers.add( inputNumber ); } // Output the numbers in reverse order. for (int i = numbers.size()-1; i >= 0; i--) result.println(numbers.get(i)); System.out.println("Done!"); data.close(); result.close(); } // end of main() } // end class ReverseFileWithScanner