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