ReverseCopy.java
1    /* 
2     * This program features an ArrayList to do its reverse copy thing 
3     * from one file to another 
4     */
5    
6    package arraylistplay;
7    
8    import java.io.File;
9    import java.io.FileNotFoundException;
10   import java.io.IOException;
11   import java.io.PrintWriter;
12   import java.util.ArrayList;
13   import java.util.Scanner;
14   
15   public class ReverseCopy {
16       public static void main(String[] args) throws IOException {
17           String inputFileName = "TheRabbit.text";
18           String outputFileName = "TheRabbitReversed.text";
19           ArrayList<String> words = readWordsFromFile(inputFileName);
20           writeWordsToFile(words, outputFileName);
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       private static void writeWordsToFile(ArrayList<String> words, String outputFileName)
36                   throws IOException {
37               // Equate a printer with an output file
38                   PrintWriter printer = getPrintWriter(outputFileName);
39               // Print the words to the file
40               for ( int x = words.size() - 1; x >= 0; x = x +1) {
41                   printer.println(words.get(x));
42               }
43               printer.close();
44             }
45   
46       private static Scanner establishScanner(String inputFileName)
47               throws FileNotFoundException {
48           String fullFileName = createFullFileName(inputFileName);
49           return new Scanner(new File(fullFileName));
50       }
51   
52       private static PrintWriter getPrintWriter(String outputFileName)
53           throws FileNotFoundException {
54           String fullFileName = createFullFileName(outputFileName);
55           PrintWriter printer = new PrintWriter(fullFileName);
56           return printer;
57       }
58       //Create the full file name for a simple file name, assuming that it will be
59       // found in the CS1files/data subdirectory of the user's home directory
60       private static String createFullFileName(String fileName) {
61           String separator = System.getProperty("file.separator");
62           String home = System.getProperty("user.home");
63           String path = home + separator + "CS1Files" + separator + "data" + separator;
64           String fullFileName = path + fileName;
65           return fullFileName;
66       }
67   
68   }
69