ReverseCopy.java
1    /* 
2    * Program featuring straight up arrays and file IO to read and reverse copy a lyric. 
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   
14   public class ReverseCopy {
15   
16           public static void main(String[] args) throws FileNotFoundException, IOException {
17                   String inputFileName = "Formula.text";
18                   String outputFileName = "FormulaReversed.text";
19                   String[] words = readWordsFromFile(inputFileName);
20                   writeWordsToFile(words, outputFileName);
21           }
22   
23           private static final int LIMIT = 1000;
24   
25           private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException {
26                   // Equate a scanner with the input file
27                   Scanner scanner = establishScanner(inputFileName);
28                   // Read the words from the file into an oversized array
29                   String[] temp = new String[LIMIT];
30                   int index = 0;
31                   while (scanner.hasNext()) {
32                           String word = scanner.next();
33                           temp[index] = word;
34                           index = index + 1;
35                   }
36   
37                   int wordCount = index;
38                   // Transfer the words to a perfectly sized array
39                   String[] words = new String[wordCount];
40                   for (int x = 0; x < wordCount; x = x + 1) {
41                           words[x] = temp[x];
42                   }
43                   // Return the words
44                   return words;
45           }
46   
47           private static void writeWordsToFile(String[] words, String outputFileName) throws IOException {
48                   // Equate a printer with an output file
49                   PrintWriter printer = getPrintWriter(outputFileName);
50                   // Print the words to the file
51                   for (int x = words.length - 1; x >= 0; x = x - 1) {
52                           printer.println(words[x]);
53                   }
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           // Create the full file name for a simple file name, assuming that it will be
71           // found in the CS1Files/data subdirectory of the user’s home directory.
72   
73           private static String createFullFileName(String fileName) {
74                   String separator = System.getProperty("file.separator");
75                   String home = System.getProperty("user.home");
76                   String path = home + separator + "CS1Files" + separator + "data" + separator;
77                   String fullFileName = path + fileName;
78                   return fullFileName;
79           }
80   }