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