ReverseCopy.java
1    /* 
2     *Program featuring straight up arrays and file IO to read and reverse copy a lyric. 
3     */
4    package arrayplay;
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.Scanner;
11   
12   
13   public class ReverseCopy {
14   
15       public static void main(String[] args) throws FileNotFoundException, IOException {
16           String inputFileName = "EndlessTeeth.text";
17           String outputFileName = "EndlessTeethReversed.text";
18           String[] words = readWordsFromFile(inputFileName);
19           writeWordsToFile(words, outputFileName);
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 file
26           Scanner scanner = establishScanner(inputFileName);
27           // Read the words from the file innto 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           }
36           int wordCount = index;
37           // transfer the words to a perfectly sized array
38           String[] words = new String[wordCount];
39           for (int x = 0; x < wordCount; x = x + 1) {
40               words[x] = temp[x];
41   
42           }
43           // Return the words
44           return words;
45   
46       }
47   
48       private static void writeWordsToFile(String[] words, String outputFileName) throws IOException {
49           // Equate a printer with an output file
50           PrintWriter printer = getPrintWriter(outputFileName);
51           // Print the words to the file
52           for ( int x = words.length - 1; x >= 0; x = x - 1 ) {
53               printer.println(words[x]);
54           }
55           printer.close();
56   
57       }
58   
59       private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
60           String fullFileName = createFullFileName(inputFileName);
61           return new Scanner(new File(fullFileName));
62       }
63   
64       private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
65           String fullFileName = createFullFileName(outputFileName);
66           PrintWriter printer = new PrintWriter(fullFileName);
67           return printer;
68   
69       }
70   
71       private static String createFullFileName(String fileName) {
72           String separator = System.getProperty("file.separator");
73           String home = System.getProperty("user.home");
74                   String path = home + separator + "CS1Files" + separator + "data" + separator;
75           String fullFileName = path + fileName;
76           return fullFileName;
77       }
78   
79   }
80   
81   
82   
83   
84   
85   
86   
87