ReverseCopy.java
1    /* 
2     * Program featuring straight up arrays and file IO to read and reverse copy a lyric. 
3     */
4    package arrayplay;
5    
6    import java.io.File;
7    import java.io.FileNotFoundException;
8    import  java.io.IOException;
9    import java.io.PrintWriter;
10   import java.util.Scanner;
11   
12       public class ReverseCopy {
13   
14           public static void main(String[] args) throws FileNotFoundException, IOException {
15               String inputFileName = "The man who sold the world.txt";
16               String outputFileName = "The man who sold the world Reversed.text";
17               String[] words = readWordsFromFile(inputFileName);
18               writeWordsToFile(words,outputFileName);
19           }
20           private static final int LIMIT = 1000;
21           private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException {
22               Scanner scanner = establishScanner(inputFileName);
23               String[] temp = new String[LIMIT];
24               int index = 0;
25               while ( scanner.hasNext() ) {
26                   String word = scanner.next();
27                   temp[index] = word;
28                   index = index + 1;
29               }
30               int wordCount = index;
31               String[] words = new String[wordCount];
32               for ( int x = 0; x < wordCount; x = x + 1 ) {
33                   words[x] = temp[x];
34               }
35   
36               return words;
37           }
38           private static void writeWordsToFile(String[] words, String outputFileName) throws IOException {
39               PrintWriter printer = getPrintWriter(outputFileName);
40   
41               for ( int x = words.length-1; x >= 0; x = x - 1 ) {
42                   printer.println(words[x]);
43               }
44               printer.close();
45           }
46           private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
47               String fullFileName = createFullFileName(inputFileName);
48               return new Scanner(new File(fullFileName));
49           }
50           private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
51               String fullFileName = createFullFileName(outputFileName);
52               PrintWriter printer = new PrintWriter(fullFileName);
53               return printer;
54           }
55           private static String createFullFileName(String fileName) {
56               String separator = System.getProperty("file.separator");
57               String home = System.getProperty("user.home");
58               String path = home + separator + "CS1Files" + separator + "data" + separator;
59               String fullFileName = path + fileName;
60               return fullFileName;
61           }
62       }
63   
64