ReverseCopy1.java
1    /* 
2     *Program featuring straight up arrays and file IO to read and reverse copy a 
3     */
4    
5       package arrayplay;
6    
7        import java.io.File;
8        import java.io.FileNotFoundException;
9        import java.io.IOException;
10       import java.io.PrintWriter;
11       import java.util.Scanner;
12   
13       public class ReverseCopy1 {
14         public static void main(String [] args) throws IOException {
15                      String inputFileName = "BigDrip.text";
16                      String outputFileName = "BigDripreversed.text";
17                      String[] words = readWordsFromFile(inputFileName);
18                      writeWordsToFile(words,outputFileName);
19                  }
20          private static final int LIMIT = 1000;
21   
22                  private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException {
23                      //Equate a scanner with the input file
24                      Scanner scanner = establishScanner(inputFileName);
25                      //Read the words from the file into and 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                      //Transfer the words to a perfectly sized array
35                     String[] words=new String[wordCount];
36                     for (int x = 0; x<wordCount; x = x + 1){
37                              words[x] = temp[x];
38                         }
39                      //Return the words;
40                     return words;
41                  }
42   
43                  private static void writeWordsToFile(String[] words,String outputFileName) throws IOException{
44                     //Equate a printer with and output file
45                      PrintWriter printer = getPrintWriter(outputFileName);
46                     //Print the words to the file
47                     for ( int x = words.length-1;x >= 0;x = x - 1){
48                              printer.println(words[x]);
49                         }
50                      printer.close();
51                  }
52   
53                  private static Scanner establishScanner(String inputFileName) throws FileNotFoundException{
54                      String fullFileName = createFullFileName(inputFileName);
55                      return new Scanner(new File(fullFileName));
56                 }
57          private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException{
58                      String fullFileName = createFullFileName(outputFileName);
59                      PrintWriter printer = new PrintWriter(fullFileName);
60                      return printer;
61                  }
62   
63                  //Create the full file name a simple file name, assuming that it will be
64                  //found in the CS1Files/data subdirectory of the user's home directory
65   
66                  private static String createFullFileName(String fileName){
67                      String separator = System.getProperty("file.separator");
68                      String home = System.getProperty("user.home");
69                      String path = home + separator + "CS1Files" + separator + "data" + separator;
70                      String fullFileName = path + fileName;
71                      return fullFileName;
72                  }
73       }
74