ReverseCopy.java
1    package arraylistplay;
2    
3    import java.io.File;
4    import java.io.FileNotFoundException;
5    import java.io.IOException;
6    import java.io.PrintWriter;
7    import java.util.ArrayList;
8    import java.util.Scanner;
9    
10   public class ReverseCopy {
11       public static void main (String[] args) throws FileNotFoundException, IOException{
12           String inputFileName = "IsThereSomewhere.text";
13           String outputFileName = "IsThereSomewhereReversed.text";
14           ArrayList<String> words = readWordsFromFile(inputFileName);
15           writeWordsFromFile(words, outputFileName);
16       }
17   
18       private static ArrayList<String> readWordsFromFile(String inputFileName) throws FileNotFoundException {
19           Scanner scanner = establishScanner(inputFileName);
20           ArrayList<String> words = new ArrayList<>();
21           while (scanner.hasNext()){
22               String word = scanner.next();
23               words.add(word);
24           }
25           return words;
26       }
27   
28       private static void writeWordsFromFile(ArrayList<String>words, String outputFileName) throws IOException {
29           PrintWriter printer = getPrintWriter(outputFileName);
30           for ( int x = words.size()-1; x >=0; x= x-1){
31               printer.println(words.get(x));
32           }
33           printer.close();
34       }
35       private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
36           String fullFileName = createFullFileName(inputFileName);
37           return new Scanner(new File(fullFileName));
38       }
39   
40       private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
41           String fullFileName = createFullFileName(outputFileName);
42           PrintWriter printer = new PrintWriter(fullFileName);
43           return printer;
44       }
45   
46       private static String createFullFileName(String fileName) {
47           String separator = System.getProperty("file.separator");
48           String home = System.getProperty("user.home");
49           String path = home + separator + "CS1Files" + separator + "data" + separator;
50           String fullFileName = path + fileName;
51           return fullFileName;
52       }
53   }
54   
55