001    
002    import java.io.*;
003    import java.util.LinkedList;
004    
005    /**
006     * Exercise 12.1 in the book.
007     * 
008     * @author Kristian Torp, torp (at) cs (dot) aau (dot) dk
009     * @version 1.0
010     */
011    public class Exercise12_1 {
012    
013            public static void main(String[] args) {
014                    // specify a file name you know exists (this win32 notation)
015                    String fileName = "c:\\temp\\test.txt";
016    
017                    // place to store lines that are read
018                    LinkedList ll = new LinkedList();
019                    BufferedReader in = null;
020                    try {
021                            // get the input
022                            in = new BufferedReader(new FileReader(fileName));
023                            String line;
024                            // loop through the lines in the file and add to linkedlist
025                            while ((line = in.readLine()) != null) {
026                                    ll.add(line);
027                            }
028    
029                            // write the lines in reverse order
030                            for (int i = ll.size() - 1; i >= 0; i--) {
031                                    line = (String) ll.get(i); // downcast to a string
032                                    System.out.println(line);
033                            }
034                    } catch (IOException e) {
035                            // just report that bad stuff happened
036                            System.err.println(e);
037                    } finally {
038                            // close the file
039                            if (in != null) {
040                                    try {
041                                            in.close();
042                                    } catch (IOException e) {
043                                            System.err.println(e);
044                                    }
045                            }
046                    }
047            }
048    }