ReverseCopy.java
1        /*2     * Program featuring straight up arrays and file IO to read and reverse copy a lyric.3     */
2    
3        package arrayplay;
4    
5        import java.io.File;
6        import java.io.FileNotFoundException;
7        import java.io.IOException;
8        import java.io.PrintWriter;
9        import java.util.Scanner;
10   
11       public class ReverseCopy {
12   
13   
14           public static void main(String[] args) throws FileNotFoundException, IOException {
15               String inputFileName = "song.txt";
16               String outputFileName = "songreversed.text";
17               String[] words = readWordsFromFile(inputFileName);
18               writeWordsToFile(words, outputFileName);
19   
20           }
21   
22           private static final int LIMIT = 1000;
23   
24           private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException {
25               // Equate a scanner with the input file26
26               Scanner scanner = establishScanner(inputFileName);
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 word = scanner.next();
32                   temp[index] = word;
33                   index = index + 1;
34               }
35               int wordCount = index;
36               // Transfer the words to a perfectly sized array37
37               String[] words = new String[wordCount];
38               for (int x = 0; x < wordCount; x = x + 1) {
39                   words[x] = temp[x];
40               }
41               // Return the words42
42               return words;
43           }
44           private static void writeWordsToFile(String[] words, String outputFileName) throws IOException {
45   
46               // Equate a printer with an output file47
47               PrintWriter printer = getPrintWriter(outputFileName);
48               // Print the words to the file49
49               for (int x = words.length - 1; x >= 0; x = x - 1) {
50                   printer.println(words[x]);
51               }
52               printer.close();
53           }
54   
55           private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
56               String fullFileName = createFullFileName(inputFileName);
57               return new Scanner(new File(fullFileName));
58           }
59   
60           private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
61               String fullFileName = createFullFileName(outputFileName);
62               PrintWriter printer = new PrintWriter(fullFileName);
63               return printer;
64           }
65           // Create the full file name for a simple file name, assuming that it will be67
66           // found in the CS1Files/data subdirectory of the user’s home directory.73
67   
68   
69           private static String createFullFileName(String fileName) {
70               String separator = System.getProperty("file.separator");
71               String home = System.getProperty("user.home");
72               String path = home + separator + "public_html" + separator + "data" + separator;
73               String fullFileName = path + fileName;
74               return fullFileName;
75   
76           }
77       }