/home/ssingh6/NetBeansProjects/CS1/src/arrayplay/ReverseCopy.java
 1 /*
 2  * To change this license header, choose License Headers in Project Properties.
 3  * To change this template file, choose Tools | Templates
 4  * and open the template in the editor.
 5  */
 6 package arrayplay;
 7 
 8 import java.io.File;
 9 import java.io.FileNotFoundException;
10 import java.io.IOException;
11 import java.io.PrintWriter;
12 import java.util.Scanner;
13 
14 /**
15  *
16  * @author ssingh6
17  */
18 public class ReverseCopy {
19 
20     /**
21      * @param args the command line arguments
22      */
23     public static void main(String[] args) throws FileNotFoundException, IOException {
24         String inputFileName = "Attention.txt";
25          String outputFileName = "AttentionReversed.txt";
26          String[] words = readWordsFromFile(inputFileName);
27          writeWordstoFile(words,outputFileName);
28      }
29      
30      private static final int LIMIT = 1000;
31  
32      private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException {
33          //Equate a scanner with the input file
34          Scanner scanner = establishScanner(inputFileName);
35          //Read the words from the file into an oversized array
36          String[] temp = new String[LIMIT];
37          int index = 0;
38          while ( scanner.hasNext() ) {
39              String word = scanner.next();
40              temp[index] = word;
41             index = index + 1;
42         }
43          int wordCount = index;
44          //Transfer the words to a perfectly sized array
45          String[] words = new String[wordCount];
46          for ( int x = 0; x < wordCount; x = x + 1 ) {
47              words[x] = temp[x];
48          }
49          //Return the words
50          return words;
51      }
52  
53      private static void writeWordstoFile(String[] words, String outputFileName) throws IOException {
54          //Equate a printer with an output file
55          PrintWriter printer = getPrintWriter(outputFileName);
56         //Print the words to the file
57         for ( int x = words.length-1; x >= 0; x = x - 1) {
58             printer.println(words[x]);
59          }
60          printer.close();
61      }
62  
63      private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
64          String fullFileName = createFullFileName(inputFileName);
65          return new Scanner(new File(fullFileName));
66      }
67  
68      private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
69          String fullFileName = createFullFileName(outputFileName);
70          PrintWriter printer = new PrintWriter(fullFileName);
71          return printer;
72      }
73      
74      //Create the full file name for a simple file name, assuming that it will be
75      //found in the CS1Files/data subdirectory of the user's home directory.
76  
77      private static String createFullFileName(String fileName) {
78          String separator = System.getProperty("file.separator");
79          String home = System.getProperty("user.home");
80          String path = home + separator + "CS1Files" + separator + "data" + separator;
81          String fullFileName = path + fileName;
82          return fullFileName;
83      }
84      
85  }
86  
87