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