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 = "fireFlies.txt";
13           String outputFileName = "fireFliesReversed.txt";
14           ArrayList<String> words = readWordsFromFile(inputFileName);
15           writeWordsToFile(words,outputFileName);
16   
17       }
18   
19   
20   
21       private static void writeWordsToFile(ArrayList<String> words, String outputFileName) throws IOException {
22           //equate a printer with an output file
23           PrintWriter printer = getPrintWriter(outputFileName);
24           //print words to file
25           for (int x = words.size() - 1; x>=0; x=x-1){
26               printer.println(words.get(x));
27           }
28           printer.close();
29   
30       }
31   
32       private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException{
33           String fullFileName = createFullFileName(outputFileName);
34           PrintWriter printer = new PrintWriter(fullFileName);
35           return printer;
36       }
37   
38       private static ArrayList<String> readWordsFromFile(String inputFileName) throws FileNotFoundException {
39           //equate a scanner with the input file
40           Scanner scanner = establishScanner(inputFileName);
41           //read the words from the file into a 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 words;
48   
49       }
50   
51       private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
52           String fullFileName = createFullFileName(inputFileName);
53           return new Scanner(new File(fullFileName));
54       }
55   
56       private static String createFullFileName(String fileName) {
57           String separator = System.getProperty("file.separator");
58           String home = System.getProperty("user.home");
59           String path = home + separator +"Documents" + separator + "public_html" + separator + "CS1WorkSite" + separator + "data" +separator;
60           String fullFileName = path + fileName;
61           return fullFileName;
62   
63       }
64   }
65