/home/evankemp/NetBeansProjects/CS1/src/arraylistplay/ReverseCopy.java
 1 /*
 2  * This program features an ArrayList to do its reverse copy thing from one file to another.
 3  */
 4 package arraylistplay;
 5 
 6 import java.io.File;
 7 import java.io.FileNotFoundException;
 8 import java.io.IOException;
 9 import java.io.PrintWriter;
10 import java.util.ArrayList;
11 import java.util.Scanner;
12 
13 /**
14  *
15  * @author evankemp
16  */
17 public class ReverseCopy {
18 
19     /**
20      * @param args the command line arguments
21      */
22     public static void main(String[] args) throws FileNotFoundException, IOException {
23         String inputFileName = "UnCrocodile.text";
24         String outputFileName = "UnCrocodileReversed.text";
25         ArrayList<String> words = readWordsFromFile(inputFileName);
26         writeWordsToFile(words,outputFileName);          
27     }
28 
29     private static ArrayList<String> readWordsFromFile(String inputFileName) throws FileNotFoundException {
30         //Equate a scanner with the input file
31         Scanner scanner = establishScanner(inputFileName);
32         //Read the words rom the file into a dynamically growing ArrayList
33         ArrayList<String> words = new ArrayList<>();
34         while (scanner.hasNext()) {
35             String word = scanner.next();
36             words.add(word);
37         }
38         //Return the words
39         return words;
40     }
41 
42     private static void writeWordsToFile(ArrayList<String> words, String outputFileName) throws IOException {
43         //Equate a printer with an output file
44         PrintWriter printer = getPrintWriter(outputFileName);
45         //Print the words to file
46         for (int x = words.size()-1; x >= 0; x = x -1){
47             printer.println(words.get(x));
48         }
49         printer.close();
50     }
51 
52     private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
53         String fullFileName = createFullFileName(inputFileName);
54         return new Scanner(new File(fullFileName));
55     }
56 
57     private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
58         String fullFileName = createFullFileName(outputFileName);
59         PrintWriter printer = new PrintWriter(fullFileName);
60         return printer;
61     }
62 
63     //Create the full file name for a simple file name, assuming that it will be
64     // found in the CS1Files/data subdirectory of the user's home directory.
65     private static String createFullFileName(String fileName) {
66         String separator = System.getProperty("file.separator");
67         String home = System.getProperty("user.home");
68         String path = home + separator + "CS1Files" + separator + "data" + separator;
69         String fullFileName = path + fileName;
70         return fullFileName; 
71     }
72     
73 }
74