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