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