ReverseCopy.java
1    /* 
2    * This program will reads words from one file and writes them in reverse order to a second file, which also means that 
3    * this program will features an ArrayList to do its reverse copy thing from one file to. 
4     */
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   public class ReverseCopy {
16       public static void main(String[]args) throws FileNotFoundException, IOException{
17           String inputFileName= "HigherLove.text";
18           String outputFileName="HigherLoveReversed.text";
19           ArrayList<String>words= readWordsFromFile(inputFileName);
20           writeWordsToFile(words, outputFileName);
21   
22   
23       }
24       private static ArrayList<String> readWordsFromFile(String inputFilename) throws FileNotFoundException{
25           //Equate a scanner with the input file
26           Scanner scanner= establishScanner(inputFilename);
27           //read the words from the file into a dynamically growing ArrayList
28           ArrayList<String> words= new ArrayList<>();
29           while (scanner.hasNext()){
30               String word= scanner.next();
31               words.add(word);
32   
33           }
34           //Return the words
35           return words;
36       }
37   
38   
39       private static void writeWordsToFile(ArrayList<String> words, String outputFileName) throws IOException {
40           //Equate a printer with an output file
41           PrintWriter printer= getPrintWriter(outputFileName);
42           //print the words to the file
43           for (int x= words.size()-1; x>= 0; x=x-1){
44               printer.println(words.get(x));
45           }
46           printer.close();
47       }
48   
49       private static Scanner establishScanner(String inputFilename) throws FileNotFoundException{
50           String fullFileName= createFullFileName(inputFilename);
51           return new Scanner(new File(fullFileName));
52   
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   // Create the full file name for a simple file name, assming that it will be
61       // found in the CS1Files/data subdirectory of the user's home directory
62       private static String createFullFileName(String filename)   {
63           String seperator= System.getProperty("file.separator");
64           String home= System.getProperty("user.home");
65           String path= home+ seperator+ "CS1Files"+ seperator+ "data"+ seperator;
66           String fullFileName= path  + filename;
67           return fullFileName;
68   
69       }
70   
71   }
72