/home/akc/NetBeansProjects/CS1/src/arrayplay/ReverseCopy.java
 1 /*
 2  * Program featuring straight up arrays and file IO to read and reverse copy a
 3  * lyric - Her Majesty by The Beatles.
 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 /**
14  *
15  * @author akc
16  */
17 public class ReverseCopy {
18 
19     /**
20      * @param args the command line arguments
21      */
22     public static void main(String[] args) throws FileNotFoundException, IOException{
23         String inputFileName = "LucyInTheSkyWithDiamonds.text";
24         String outputFileName = "LucyInTheSkyWithDiamondsReversed.text";
25         String [] words = readWordsFromFile (inputFileName);
26         writeWordsToFile (words, outputFileName);
27     }
28     
29     private static final int LIMIT = 1000;
30 
31     private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException{
32         // Equate a scanner with the input file
33         Scanner scanner = establishScanner(inputFileName);
34         // Read the words from the file into an oversized array
35         String[] temp = new String[LIMIT];
36         int index = 0;
37         while ( scanner.hasNext() ) {
38             String word = scanner.next();
39             temp[index] = word;
40             index = index + 1;
41         }
42         int wordCount = index;
43         // Transfer the words to a perfectly sized array
44         String[] words = new String[wordCount];
45         for ( int x = 0; x < wordCount; x = x + 1 ){
46             words[x] = temp [x];
47         }
48         // Return the words
49         return words;
50     }
51 
52     private static void writeWordsToFile(String[] words, String outputFileName) throws IOException {
53         // Equate a painter with an output file
54         PrintWriter printer = getPrintWriter(outputFileName);
55         // Print the words to the file
56         for ( int x = words.length-1; x >= 0; x = x -1 ){
57             printer.println(words[x]);
58         }
59         printer.close();
60     }
61 
62     private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
63         String fullFileName = createFullFileName(inputFileName);
64         return new Scanner(new File(fullFileName));
65     }
66 
67     private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
68         String fullFileName = createFullFileName(outputFileName);
69         PrintWriter printer = new PrintWriter(fullFileName);
70         return printer;
71     }
72             
73     //Create the full file name for a simple file name, assuming that it will be
74     //found in the CS1Files/data subdirectory of the user's home directory.
75     private static String createFullFileName(String fileName) {
76         String separator = System.getProperty("file.separator");
77         String home = System.getProperty("user.home");
78         String path = home + separator + "CS1Files" + separator + "data" + separator;
79         String fullFileName = path + fileName;
80         return fullFileName;
81     }
82     
83 }
84