ReverseCopy.java
1    /* 
2    * Program featuring straight up arrays and file IO to read and reverse copy a lyric. 
3     */
4    
5    
6    
7    package arrayplay;
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       public static void main(String[] args) throws FileNotFoundException, IOException {
16           String inputFileName = "KDASong.text";
17           String outputFileName = "KDASongReverse.text";
18           String[] words = readWordsFromFile(inputFileName);
19           writeWordsToFile(words, outputFileName);
20   
21       }
22   
23       private static final int LIMIT = 100;
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           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 words;
43       }
44   
45           private static void writeWordsToFile (String[] words, String outputFileName) throws IOException {
46               //Equate a printer with an output file
47               PrintWriter printer = getPrintWriter(outputFileName);
48               // Print the words to the file
49               for (int x = words.length - 1; x >= 0; x = x - 1) {
50                   printer.println(words[x]);
51               }
52               printer.close();
53           }
54           private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
55               String fullFileName = createFullFileName(inputFileName);
56               return new Scanner(new File(fullFileName));
57           }
58           private static PrintWriter getPrintWriter (String outputFileName) throws FileNotFoundException {
59               String fullFileName = createFullFileName(outputFileName);
60               PrintWriter printer = new PrintWriter(fullFileName);
61               return printer;
62           }
63           // Create the full file name for a simple file name, assuming that it will be
64           // found in the CS1Files/data subdirectory of the user's home directory.
65           private static  String createFullFileName(String fileName){
66               String separator = System.getProperty("file.separator");
67               String home = System.getProperty("user.home");
68               String path = home + separator + "CS1Files" + separator + "data" + separator;
69               String fullFileName = path + fileName;
70               //System.out.println("FULLFILENAME:" + fullFileName);
71               return fullFileName;
72           }
73   
74   
75       }
76   
77