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       public static void main(String[] args) throws FileNotFoundException, IOException {
12           String inputFileName = "NoLove.text";
13           String outputFileName = "NoLoveReversed.text";
14           ArrayList<String> words = readWordsFromFile(inputFileName);
15           writeWordsToFile(words, outputFileName);
16       }
17   
18       private static ArrayList<String> readWordsFromFile(String inputFileName) throws FileNotFoundException {
19           // Equate a scanner with the input file
20           Scanner scanner = establishScanner(inputFileName);
21           // Read the words from the file into an oversized array
22           ArrayList<String> words = new ArrayList<>();
23           int index = 0;
24           while (scanner.hasNext()) {
25               String word = scanner.next();
26               words.add(word);
27           }
28           // Return the words
29           return words;
30       }
31   
32       private static void writeWordsToFile(ArrayList<String> words, String outputFileName) throws IOException {
33           // Equate a printer with an output file
34           PrintWriter printer = getPrintWriter(outputFileName);
35           // Print the words to the file
36           for (int x = words.size() - 1; x >= 0; x = x - 1) {
37               printer.println(words.get(x));
38           }
39           printer.close();
40       }
41   
42       private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
43           String fullFileName = createFullFileName(inputFileName);
44           return new Scanner(new File(fullFileName));
45       }
46       private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
47           String fullFileName = createFullFileName(outputFileName);
48           PrintWriter printer = new PrintWriter(fullFileName);
49           return printer;
50       }
51       private static String createFullFileName(String fileName) {
52           String separator = System.getProperty("file.separator");
53           String home = System.getProperty("user.home");
54           String path = home + separator + "CS1Files" + separator + "data" + separator;
55           String fullFileName = path + fileName;
56           return fullFileName;
57       }
58   }