1 package arraylistplay; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.util.ArrayList; 6 import java.io.IOException; 7 import java.io.PrintWriter; 8 import java.util.Scanner; 9 10 public class ReverseCopy { 11 public static void main(String[] args) throws FileNotFoundException, IOException { 12 String inputFileName = "Codex.text"; 13 String outputFileName = "CodexReversed.text"; 14 ArrayList<String> words = readWordsFromFile(inputFileName); 15 writeWordsToFile(words, outputFileName); 16 17 } 18 19 private static ArrayList<String> readWordsFromFile(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 writeWordsToFile(ArrayList<String> words, String outputFileName) throws IOException { 30 PrintWriter printer = getPrintWriter(outputFileName); 31 for (int x = words.size() - 1; x >= 0; 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 58