ReverseCopy.java
1    package arrayPlay;
2    
3    import java.io.File;
4    import java.io.FileNotFoundException;
5    import java.io.IOException;
6    import java.util.Scanner;
7    import java.io.PrintWriter;
8    
9    public class ReverseCopy {
10   
11       public static void main (String[] args) throws FileNotFoundException, IOException {
12           String inputFileName = "LifeIsAHighway.text";
13           String outputFileName = "LifeIsAHighwayReversed.text";
14           String [] words = readWordsFromFile(inputFileName);
15           writeWordsToFile(words, outputFileName);
16   
17       }
18   
19       private static final int LIMIT = 1000;
20   
21       private static String [] readWordsFromFile(String inputFileName) throws FileNotFoundException {
22           //Equate a scanner with the input file
23           Scanner scanner = establishScanner(inputFileName);
24   
25           //Read the words from the file into an oversized array
26           String[] temp = new String[LIMIT];
27           int index = 0;
28           while (scanner.hasNext()) {
29               String word = scanner.next();
30               temp[index] = word;
31               index = index + 1;
32           }
33           int wordCount = index;
34   
35           //Transfer the words to a perfectly sized array
36           String [] words = new String[wordCount];
37           for (int x = 0; x < wordCount; x = x + 1) {
38               words[x] = temp[x];
39           }
40           //Return the words
41           return words;
42       }
43   
44       private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
45           String fullFileName = createFullFileName(inputFileName);
46           return new Scanner(new File(fullFileName));
47   
48       }
49   
50       private static void writeWordsToFile(String[] words, String outputFileName) throws IOException {
51           //Equate a printer with an output file
52           PrintWriter printer = getPrintWriter(outputFileName);
53           //Print words to the file
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 PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException{
61           String fullFileName = createFullFileName(outputFileName);
62           PrintWriter printer = new PrintWriter(fullFileName);
63           return printer;
64       }
65   
66       //Create the full name for a simple file name, assuming that it will be
67       //found in the CS1Files/data subdirectory of the user's home directory
68       private static String createFullFileName(String fileName) {
69           String separator = System.getProperty("file.separator");
70           String home = System.getProperty("user.home");
71           String path = home + separator + "CS1Files" + separator + "data" + separator;
72           String fullFileName = path + fileName;
73           return fullFileName;
74   
75       }
76   }
77