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