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