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