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   public class ReverseCopy {
14   
15       public static void main(String[] args) throws FileNotFoundException, IOException {
16   
17           String inputFileName = "IslandInTheSun.text";
18           String outputFilename = "IslandInTheSunReversed.text";
19           String[] words = readWordsFromFile(inputFileName);
20           writeWordsToFile(words, outputFilename);
21   
22       }
23   
24       private static final int LIMIT = 1000;
25   
26       private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException {
27           //Equate a scanner with the input file
28           Scanner scanner = establishScanner(inputFileName);
29           //Read the words from the file into an oversized array
30           String[] temp = new String[LIMIT];
31           int index = 0;
32           while(scanner.hasNext()) {
33               String word = scanner.next();
34               temp[index] = word;
35               index = index + 1;
36   
37           }
38   
39           int wordCount = index;
40           //Transfer the words to a perfectly sized array
41           String[] words = new String[wordCount];
42           for(int x = 0; x < wordCount; x = x + 1) {
43               words[x] = temp[x];
44           }
45           //Return the words
46           return words;
47       }
48   
49       private static void writeWordsToFile(String[] words, String outputFileName) throws FileNotFoundException {
50           //Equate a printer with an output file
51           PrintWriter printer = getPrintWriter(outputFileName);
52           //Print the words to the file
53           for (int x = words.length -1; x >= 0; x = x - 1) {
54               printer.println(words[x]);
55   
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       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