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   
12       public static void main(String[] args) throws FileNotFoundException, IOException {
13           String inputFileName = "5200.text";
14           String outputFileName = "5200reversed.text";
15           ArrayList<String> words = readWordsFromFiles(inputFileName);
16           writeWordsToFiles(words, outputFileName);
17       }
18   
19       private static ArrayList<String> readWordsFromFiles(String inputFileName) throws FileNotFoundException {
20           Scanner scanner = establishScanner(inputFileName);
21           ArrayList<String> words = new ArrayList<>();
22           while (scanner.hasNext()) {
23               String word = scanner.next();
24               words.add(word);
25           }
26           return words;
27       }
28   
29       private static void writeWordsToFiles(ArrayList<String> words, String outputFileName) throws IOException {
30           PrintWriter printer = getPrintWriter(outputFileName);
31           for (int x = words.size() - 1; x >= 0; x = x - 1) {
32               printer.println(words.get(x));
33           }
34           printer.close();
35       }
36   
37       private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
38           String fullFileName = createFullFileName(inputFileName);
39           return new Scanner(new File(fullFileName));
40       }
41   
42       private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
43           String fullFileName = createFullFileName(outputFileName);
44           PrintWriter printer = new PrintWriter(fullFileName);
45           return printer;
46       }
47   
48       private static String createFullFileName(String fileName) {
49           String separator = System.getProperty("file.separator");
50           String home = System.getProperty("user.home");
51           String path = home + separator + "CS1Files" + separator + "data" + separator;
52           String fullFileName = path + fileName;
53           return fullFileName;
54       }
55   
56   }
57