ReverseCopy.java
1    /* 
2    * This program features an ArrayList to do its reverse copy thing from one file to another. 
3     */
4    
5    
6    package arraylistplay;
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   public class ReverseCopy {
16       public static void main(String[] args) throws FileNotFoundException, IOException {
17           String inputFileName = "BTSLyrics.text";
18           String outputFileName = "BTSLyricsReversed.text";
19           ArrayList<String> words = readWordsFromFile(inputFileName);
20           writeWordsToFile(words, outputFileName);
21       }
22   
23       private static  ArrayList<String> readWordsFromFile(String inputFileName)
24           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           //Return the words
34           return words;
35       }
36       private static void writeWordsToFile(ArrayList<String>words, String outputFileName)
37           throws IOException{
38           //Equate a printer with an output file
39           PrintWriter printer= getPrintWriter(outputFileName);
40           //Print the words to the File
41           for(int x=words.size()-1;x>=0; x=x-1) {
42               printer.println(words.get(x));
43           }
44           printer.close();
45       }
46   
47       private static Scanner establishScanner(String inputFileName)
48           throws FileNotFoundException{
49           String fullfileName= createFullFileName(inputFileName);
50           return new Scanner(new File(fullfileName));
51       }
52   
53       private static PrintWriter getPrintWriter(String outputFileName)
54           throws FileNotFoundException{
55           String fullFileName= createFullFileName(outputFileName);
56           PrintWriter printer= new PrintWriter(fullFileName);
57           return printer;
58       }
59   
60       private static String createFullFileName(String fileName) {
61           String separator= System.getProperty("file.separator");
62           String home= System.getProperty("user.home");
63           String path= home+ separator+"CS1Files"+ separator+"data"+ separator;
64           String fullFilename= path + fileName;
65           return fullFilename;
66   
67       }
68   
69   }
70