ReverseCopy.java
1    package arrayplay;
2    
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    
9    public class ReverseCopy {
10   
11       public static void main(String[] args) throws FileNotFoundException, IOException {
12           String inputFileName = "IWriteSinsNotTragedies.text";
13           String outputFileName = "IWriteSinsNotTragediesReversed.text";
14           String[] words = readWordsFromFile(inputFileName);
15           writeWordsToFile(words,outputFileName);
16       }
17   
18       private static final int LIMIT = 1000;
19   
20       private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException {
21   
22           Scanner scanner = establishScanner(inputFileName);
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   
33           String[] words = new String[wordCount];
34           for ( int x = 0; x < wordCount; x = x + 1) {
35               words[x] = temp[x];
36           }
37   
38           return words;
39       }
40   
41       private static void writeWordsToFile(String[] words, String outputFileName) throws IOException {
42           PrintWriter printer = getPrintWriter(outputFileName);
43           for ( int x = words.length - 1; x >= 0; x = x - 1 ) {
44               printer.println(words[x]);
45           }
46   
47           printer.close();
48       }
49   
50       private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
51           String fullFileName = createFullFileName(inputFileName);
52           return new Scanner(new File(fullFileName));
53       }
54   
55       private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
56           String fullFileName = createFullFileName(outputFileName);
57           PrintWriter printer = new PrintWriter(fullFileName);
58           return printer;
59       }
60   
61       private static String createFullFileName(String fileName) {
62           String separator = System.getProperty("file.separator");
63           String home = System.getProperty("user.home");
64           String path = home + separator + "CS1Files" + separator + "data" +separator;
65           String fullFileName = path + fileName;
66           return fullFileName;
67       }
68   }
69