/home/sjenks/NetBeansProjects/CS2/src/textprocessing/Reverse.java
 1 /*
 2  * This program provides you with ideas about simple text prcoessing and file 
 3  * processing.
 4  */
 5 package textprocessing;
 6 
 7 import java.io.File;
 8 import java.io.FileNotFoundException;
 9 import java.io.IOException;
10 import java.util.ArrayList;
11 import java.util.Scanner;
12 import javax.swing.JFileChooser;
13 
14 /**
15  *
16  * @author sjenks
17  */
18 public class Reverse {
19 
20     /**
21      * @param args the command line arguments
22      */
23     public static void main(String[] args) throws FileNotFoundException, IOException {
24         Scanner scanner = getProperty();
25         ArrayList<String> words = readWordsFromFile(scanner);
26         display(words);
27 
28     }
29 
30     private static Scanner getProperty() throws FileNotFoundException {
31         String homedir = System.getProperty("user.home");
32         System.out.println("homedir = " + homedir);
33         JFileChooser jfc = new JFileChooser(new File(homedir));
34         jfc.showOpenDialog(null);
35         File file = jfc.getSelectedFile();
36         Scanner scanner = new Scanner(file);
37         return scanner;
38     }
39 
40     private static ArrayList<String> readWordsFromFile(Scanner scanner) throws FileNotFoundException {
41         ArrayList<String> words = new ArrayList<>();
42         while (scanner.hasNext()) {
43             String word = scanner.next();
44             words.add(word);
45         }
46         return words;
47     }
48 
49     private static void display(ArrayList<String> words) {
50         System.out.println(">>> ");
51         for (int x = words.size() - 1; x >= 0; x = x - 1) {
52             System.out.println(words.get(x));
53         }
54     }
55 }
56