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