ReverseCopy.java
1    package arrays;
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   public class ReverseCopy {
11       public static void main(String[] args) throws FileNotFoundException, IOException {
12           String inputFileName = "EineKleine.text";
13           String outputFileName = "EineKleineReversed.text";
14           String[] words = readWordsFromFile(inputFileName);
15           writeWordsToFile(words,outputFileName);
16       }
17       private static final int LIMIT = 1000;
18   
19       private static String[] readWordsFromFile (String inputFileName) throws FileNotFoundException {
20           //Equate a scanner with the input file
21           Scanner scanner = establishScanner(inputFileName);
22           //Read the words from the file into an over-sized array.
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           }
31           int wordCount = index;
32           //Transfer the words to a perfectly sized array.
33           String[] words = new String[wordCount];
34           for (int x = 0; x < wordCount; x = x + 1) {
35               words[x] = temp[x];
36               System.out.println(words[x]);
37           }
38           //Return the words...
39           return words;
40   
41       }
42       private static void writeWordsToFile(String[] words, String outputFileName) throws IOException {
43           //Equate a printer with an output file.
44           PrintWriter printer = getPrinterWriter(outputFileName);
45           //Print the words to the file.
46           for (int x = words.length-1; x >= 0; x = x - 1) {
47               printer.println(words[x]);
48           }
49           printer.close();
50       }
51       private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
52           String fullFileName = createFullFileName(inputFileName);
53           return new Scanner(new File(fullFileName));
54       }
55       private static PrintWriter getPrinterWriter(String outputFileName) throws FileNotFoundException {
56           String fullFileName = createFullFileName(outputFileName);
57           PrintWriter printer = new PrintWriter(fullFileName);
58           return printer;
59       }
60       //Create the full file name for a simple file name, assuming that it will be
61       //found in the CS1File/data subdirectory of the user's home directory.
62       private static String createFullFileName(String fileName) {
63           String separator = System.getProperty("file.separator");
64           String home = System.getProperty("user.home");
65           String path = home + separator + "CS1Files" + separator + "data" + separator;
66           String fullFileName = path + fileName;
67           return fullFileName;
68   
69       }
70   }
71   //This file still needs some work...