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