/home/ssingh6/NetBeansProjects/CS1/src/arraylistplay/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 arraylistplay;
 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.ArrayList;
13 import java.util.Scanner;
14 
15 /**
16  *
17  * @author ssingh6
18  */
19 public class ReverseCopy {
20 
21     /**
22      * @param args the command line arguments
23      */
24     public static void main(String[] args) throws FileNotFoundException, IOException {
25         String inputFileName = "HowLong.txt";
26          String outputFileName = "HowLongReversed.txt";
27          ArrayList<String> words = readWordsFromFile(inputFileName);
28          writeWordstoFile(words,outputFileName);
29      }
30  
31      private static ArrayList<String> readWordsFromFile(String inputFileName) throws FileNotFoundException {
32          //Equate a scanner with the input file
33          Scanner scanner = establishScanner(inputFileName);
34          //Read the words from the file into a dynamically growing ArrayList
35         ArrayList<String> words = new ArrayList<>();
36          while ( scanner.hasNext() ) {
37              String word = scanner.next();
38              words.add(word);
39          }
40         //Return the words
41          return words;
42      }
43 
44      private static void writeWordstoFile(ArrayList<String> words, String outputFileName) throws IOException {
45         //Equate a printer with an output file
46         PrintWriter printer = getPrintWriter(outputFileName);
47         //Print the words to the file
48          for ( int x = words.size()-1; x >= 0; x = x - 1) {
49              printer.println(words.get(x));
50          }
51          printer.close();
52      }
53  
54      private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
55          String fullFileName = createFullFileName(inputFileName);
56         return new Scanner(new File(fullFileName));
57      }
58  
59      private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
60          String fullFileName = createFullFileName(outputFileName);
61          PrintWriter printer = new PrintWriter(fullFileName);
62          return printer;
63      }
64     
65      //Create the full file name for a simple file name, assuming that it will be
66      //found in the CS1Files/data subdirectory of the user's home directory.
67  
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  
78