ReverseCopy.java
1    package arraylistplay;
2    
3    import java.io.File;
4    import java.io.FileNotFoundException;
5    import java.io.IOException;
6    import java.io.PrintWriter;
7    import java.util.ArrayList;
8    import java.util.Scanner;
9    
10   public class ReverseCopy {
11   
12       public static void main(String[] args) throws FileNotFoundException, IOException {
13           String inputFileName = "GirlOnATrainLyrics.text";
14           String outputFileName = "GirlOnATrainLyricsReversed.text";
15           ArrayList<String> words = readWordsFromFile(inputFileName);
16           writeWordsToFile(words, outputFileName);
17       }
18   
19       private static void writeWordsToFile(ArrayList<String> words, String outputFileName)
20           throws  FileNotFoundException {
21           //Equate a printer with an output file
22           PrintWriter printer = getPrintWriter(outputFileName);
23           //Print the words to the file
24           for (int x = words.size() - 1; x >= 0; x = x - 1) {
25               printer.println(words.get(x));
26           }
27           printer.close();
28       }
29   
30       private static PrintWriter getPrintWriter(String outputFileName)
31               throws FileNotFoundException {
32           String fullFileName = createFullFileName(outputFileName);
33           PrintWriter printer = new PrintWriter(fullFileName);
34           return printer;
35       }
36   
37       private static ArrayList<String> readWordsFromFile(String inputFileName)
38           throws  FileNotFoundException {
39           //Equate a scanner with the input file
40           Scanner scanner = establishScanner(inputFileName);
41           //Read the words from the file into a dynamically growing Array List
42           ArrayList<String> words = new ArrayList<>();
43           while (scanner.hasNext()) {
44               String word = scanner.next();
45               words.add(word);
46           }
47           // Return the words
48           return words;
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 String createFullFileName(String fileName) {
58           String separator = System.getProperty("file.seperator");
59           String home = System.getProperty("user.home");
60           String path = home + separator + "CS1Files" + separator + "data" + separator;
61           String fullFileName = path + fileName;
62           return fullFileName;
63       }
64   
65   }
66   
67