ReverseCopy.java
1    /* 
2     * This program reverses the song lyrics types in emacs and creates a new file with the information. 
3     */
4    
5    package arrayplay;
6    
7    import java.io.File;
8    import java.io.FileNotFoundException;
9    import java.io.IOException;
10   import java.io.PrintWriter;
11   import java.util.Scanner;
12   
13   public class ReverseCopy {
14       public static void main (String[] args) throws FileNotFoundException, IOException {
15           String inputFileName = "Lyric";
16           String outputFileName = "LyricReversed";
17           String[] words = readWordsFromFile(inputFileName);
18           writeWordsToFile(words, outputFileName);
19       }
20   
21       private static final int LIMIT = 1000;
22   
23       private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException {
24           // Equate a scanner with the input file
25           Scanner scanner = establishScanner(inputFileName);
26   
27           // Read the words from the file into an oversized array
28           String[] temp = new String [LIMIT];
29           int index = 0;
30           while (scanner.hasNext()) {
31               String words = scanner.next();
32               temp[index] = words;
33               index = index + 1;
34           }
35   
36           int wordCount = index;
37   
38           // Transfer the words to a perfectly sized array
39   
40           String[] words = new String[wordCount];
41           for (int x = 0; x < wordCount; x = x + 1) {
42               words[x] = temp[x];
43           }
44   
45           // Return the words
46           return words;
47       }
48   
49       private static void writeWordsToFile(String[] words, String outputFileName) throws IOException {
50           // Equate a printer with an output file
51           PrintWriter printer = getPrintWriter(outputFileName);
52   
53           // Print the words to the file
54           for (int x = words.length - 1; x >= 0; x = x - 1) {
55               printer.println(words[x]);
56           }
57           printer.close();
58       }
59   
60       private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
61           String fullFileName = createFullFileName(inputFileName);
62           return new Scanner(new File(fullFileName));
63       }
64   
65       private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException{
66           String fullFileName = createFullFileName(outputFileName);
67           PrintWriter printer = new PrintWriter(fullFileName);
68           return printer;
69       }
70   
71       // Create the full file name for a simple file name, assuming that it will be
72       // found in the CS1Files/data subdirectory of the user's home directory.
73   
74       private static String createFullFileName(String fileName) {
75           String separator = System.getProperty("file.separator");
76           String home = System.getProperty("user.home");
77           String path = home + separator + "CS1Files" + separator + "data" + separator;
78           String fullFileName = path + fileName;
79           return fullFileName;
80       }
81   
82   }
83   
84   
85   
86   
87   
88   
89   
90   
91   
92   
93   
94   
95   
96