/* * Kyle Pierce * CSE 143 * Reads from "words.txt" and prints the lines of that file * in the reverse order from what is in the file */ import java.io.*; import java.util.*; public class ReverseFile { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("words.txt")); /* * clunky array code -- let's do better! * * String[] lines = new String[100]; * int count = 0; * while (input.hasNextLine()) { * String line = input.nextLine(); * lines[count] = line; * count++; * } * * for (int i = count - 1; i >= 0; i--) { * System.out.println(lines[i]); * } */ // read the file into the list of lines ArrayList lines = new ArrayList(); while (input.hasNextLine()) { String line = input.nextLine(); lines.add(line); } // print out the lines backwards for (int i = lines.size() - 1; i >= 0; i--) { System.out.println(lines.get(i)); } } }