1 /* 2 * This program feats. an ArrayList to do its reverse copy thing from one file to another. 3 */ 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.lang.reflect.Array; 12 import java.util.ArrayList; 13 import java.util.Scanner; 14 15 public class ReverseCopy { 16 17 public static void main(String[] args) throws FileNotFoundException, IOException { 18 19 String inputFileName = "Dodie.txt"; 20 String outputFileName = "DodieReversed.txt"; 21 ArrayList<String> words = readWordsFromFile(inputFileName); 22 writeWordsToFile(words, outputFileName); 23 24 } 25 26 private static ArrayList<String> readWordsFromFile(String inputFileName) throws FileNotFoundException { 27 28 Scanner scanner = establishScanner(inputFileName); 29 ArrayList<String> words = new ArrayList<>(); 30 while (scanner.hasNext()) { 31 32 String word = scanner.next(); 33 words.add(word); 34 35 } 36 37 return words; 38 39 } 40 41 private static void writeWordsToFile(ArrayList<String> words, String outputFileName) throws IOException { 42 43 PrintWriter printer = getPrintWriter(outputFileName); 44 for (int x = words.size() - 1; x >= 0; x = x - 1) { 45 printer.println(words.get(x)); 46 } 47 48 printer.close(); 49 50 } 51 52 private static Scanner establishScanner(String inputFileName) throws FileNotFoundException { 53 54 String fullFileName = createFullFileName(inputFileName); 55 return new Scanner(new File(fullFileName)); 56 57 } 58 59 private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException { 60 61 String fullFileName = createFullFileName(outputFileName); 62 PrintWriter printer = new PrintWriter(fullFileName); 63 return printer; 64 65 } 66 67 private static String createFullFileName(String fileName) { 68 69 String separator = System.getProperty("file.separator"); 70 String home = System.getProperty("user.home"); 71 String path = home + separator + "CS1Files" + separator + "data" + separator; 72 String fullFileName = path + fileName; 73 return fullFileName; 74 75 } 76 77 } 78