1 2 3 4 package arraylistplay; 5 import java.io.File; 6 import java.io.FileNotFoundException; 7 import java.io.IOException; 8 import java.io.PrintWriter; 9 import java.util.ArrayList; 10 import java.util.Scanner; 11 12 public class reversecopy { 13 public static void main(String[] args) throws FileNotFoundException, IOException { 14 String inputFileName = "Memories.text"; 15 String outputFileName = "MemoriesReverse.text"; 16 ArrayList<String> words = realWordsFromFile(inputFileName); 17 writeWordsToFile(words, outputFileName); 18 } 19 20 private static ArrayList<String> realWordsFromFile(String inputFileName) throws FileNotFoundException { 21 22 Scanner scanner = establishScanner(inputFileName); 23 24 ArrayList<String> words = new ArrayList<>(); 25 while (scanner.hasNext()) { 26 String word = scanner.next(); 27 words.add(word); 28 } 29 30 return words; 31 } 32 33 private static void writeWordsToFile(ArrayList<String> words, String outputFileName) throws IOException { 34 35 PrintWriter printer = getPrintWriter(outputFileName); 36 37 for (int x = words.size() - 1; x >= 0; x = x - 1) { 38 printer.println(words.get(x)); 39 } 40 printer.close(); 41 } 42 43 private static Scanner establishScanner(String inputFileName) throws FileNotFoundException { 44 String fullFileName = createFullFileName(inputFileName); 45 return new Scanner(new File(fullFileName)); 46 } 47 48 private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException { 49 String fullFileName = createFullFileName(outputFileName); 50 PrintWriter printer = new PrintWriter(fullFileName); 51 return printer; 52 } 53 54 55 private static String createFullFileName(String fileName) { 56 String separator = System.getProperty("file.separator"); 57 String home = System.getProperty("user.home"); 58 String path = home + separator + "CS1Files" + separator + "data" + separator; 59 String fullFileName = path + fileName; 60 return fullFileName; 61 } 62 } 63 64