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.net.FileNameMap;
12   import java.util.Scanner;
13   
14   public class ReverseCopy {
15   
16       public static void main(String[] args) throws FileNotFoundException, IOException {
17           String inputFileName = "GirlFromTheSidewalk.text";
18           String outputFileName = "GirlFromTheSidewalkReversed.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 over sized 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           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           //return the words
43           return words;
44       }
45   
46       private static void writeWordsToFile(String[] words, String outputFileName) throws IOException {
47           //equate a printer with an output file
48           PrintWriter printer = getPrintWriter(outputFileName);
49           //print the words to the file
50           for (int x = words.length-1; x >= 0; x=x-1) {
51               printer.println(words[x]);
52           }
53           printer.close();
54       }
55   
56       private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
57           String fullFileName = createFullFileName(inputFileName);
58           return new Scanner(new File(fullFileName));
59       }
60   
61       private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
62           String fullFileName = createFullFileName(outputFileName);
63           PrintWriter printer = new PrintWriter(fullFileName);
64           return printer;
65       }
66       //create the full file name for a simple file name, assuming that it will be
67       //found in the CS1Files/data subdirectory of the user's home directory.
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 + "CS1Files" + separator + "data" + separator;
73           String fullFileName = path + fileName;
74           return fullFileName;
75       }
76   }