ReverseCopy.java
1    
2    
3    package arraylistplay;
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   
11   public class ReverseCopy {
12       public static void main(String[] args) throws FileNotFoundException, IOException {
13           String inputFileName = "PoemForFall.text";
14           String outputFileName = "PoemForFallReversed.text";
15           String[] words = readwordsFromFile(inputFileName);
16           writeWordsToFile(words, outputFileName);
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           // Read the words from the file into an oversized array
25           String[] temp = new String[LIMIT];
26           int index = 0;
27           while (scanner.hasNext()) {
28               String word = scanner.next();
29               temp[index] = word;
30               index = index + 1;
31           }
32           int wordCount = index;
33           // Transfer the words to a perfect;y sized array
34           String[] words = new String[wordCount];
35           for (int x = 0; x < wordCount; x = x + 1) {
36               words[x] = temp[x];
37           }
38           // return words
39           return words;
40       }
41       private static void writeWordsToFile(String[] words, String outputFileName) throws IOException {
42           // Equate a printer with an output file
43           PrintWriter printer = getPrintWrite(outputFileName);
44           // pu the words to the file
45           for (int x = words.length - 1; x > 0; x = x - 1) {
46               printer.println(words[x]);
47           }
48           printer.close();
49       }
50       private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
51           String fullFileName = createFullFileName(inputFileName);
52           return new Scanner(new File(fullFileName));
53       }
54       private static PrintWriter getPrintWrite(String outputFileName) throws FileNotFoundException {
55           String fullFileName = createFullFileName(outputFileName);
56           PrintWriter printer = new PrintWriter(fullFileName);
57           return printer;
58       }
59       private static String createFullFileName(String fileName) {
60           String separator= System.getProperty("file.separator");
61           String home = System.getProperty("user.home");
62           String path = home + separator + "CS1Files" + separator + "data" + separator;
63           String fullFilename = path + fileName;
64           return fullFilename;
65       }