/home/sjenks/NetBeansProjects/CS1/src/arraylistplay/ReverseCopy.java
 1 /*
 2  * This programfeaturs an ArrayList to do its reverse copy thign from one file 
 3  * to another. 
 4  */
 5 package arraylistplay;
 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.ArrayList;
12 import java.util.Scanner; 
13 
14 /**
15  *
16  * @author sjenks
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 = "Alive.text";
25         String outputFileName = "AliveReversed.text";
26         ArrayList<String> words = readWordsFromFile(inputFileName);
27         writeWordsToFile(words,outputFileName);
28     }
29 
30     private static ArrayList<String> readWordsFromFile(String inputFileName) throws FileNotFoundException{
31         //Equate a scanner with the input file
32         Scanner scanner = establishScanner(inputFileName);
33         //Read the words from the file into a dynamically growing arrayList 
34         ArrayList <String> words = new ArrayList <> ();
35         while (scanner.hasNext()) {
36             String word = scanner.next();
37             words.add(word);
38         }
39         //Return the words
40         return words;
41     }
42 
43     private static void writeWordsToFile(ArrayList<String> words, String outputFileName) throws IOException {
44         //Equare a printer with an output file
45         PrintWriter printer = getPrintWriter(outputFileName);
46         //print the owrds to the file
47         for ( int x = words.size()-1; x >= 0; x = x-1){
48             printer.println(words.get(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 
58     private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException{
59         String fullFileName = createFullFileName(outputFileName);
60         PrintWriter printer = new PrintWriter(fullFileName);
61         return printer;
62     }
63 
64     //Create the full file name for  a simple file name, assuming that it will be
65     //found in the CS1Files/data subdirectory of the users's home directory. 
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     }
75     
76 }
77