1 /* 2 * This program features an ArrayList to do its reverse copy thing 3 * from one file to another 4 */ 5 6 package arraylistplay; 7 8 import java.io.File; 9 import java.io.FileNotFoundException; 10 import java.io.IOException; 11 import java.io.PrintWriter; 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 String inputNothingElseMatters = "NothingElseMatters.text"; 19 String outputNothingElseMatters = "NothingElseMattersReversed.text"; 20 ArrayList<String> words = readWordsFromFile(inputNothingElseMatters); 21 writeWordsToFile(words, outputNothingElseMatters); 22 } 23 24 private static ArrayList<String> readWordsFromFile(String inputNothingElseMatters) 25 throws FileNotFoundException { 26 // Equate a scanner with the input file 27 Scanner scanner = establishScanner(inputNothingElseMatters); 28 // Read the words from the file into a dramatically growing ArrayList 29 ArrayList<String> words = new ArrayList<>(); 30 while (scanner.hasNext()) { 31 String word = scanner.next(); 32 words.add(word); 33 } 34 // Return the words 35 return words; 36 } 37 38 private static void writeWordsToFile(ArrayList<String> words, String outputNothingElseMatters) 39 throws IOException { 40 // Equate a printer with an output file 41 PrintWriter printer = getPrintWriter(outputNothingElseMatters); 42 // Print the words to the file 43 for ( int x = words.size()-1; x >= 0; x = x-1 ) { 44 printer.println(words.get(x)); 45 } 46 printer.close(); 47 } 48 49 private static Scanner establishScanner(String inputNothingElseMatters) 50 throws FileNotFoundException { 51 String fullNothingElseMatters = createFullNothingElseMatters(inputNothingElseMatters); 52 return new Scanner (new File(fullNothingElseMatters)); 53 } 54 55 private static PrintWriter getPrintWriter(String outputNothingElseMatters) 56 throws FileNotFoundException { 57 String fullNothingElseMatters = createFullNothingElseMatters(outputNothingElseMatters); 58 PrintWriter printer = new PrintWriter(fullNothingElseMatters); 59 return printer; 60 } 61 62 //Create the full file name for the simple file name, assuming that it will be 63 //found in the CS1Files/data subdirectory of the user's home directory. 64 private static String createFullNothingElseMatters(String NothingElseMatters) { 65 String separator = System.getProperty("file.separator"); 66 String home = System.getProperty("user.home"); 67 String path = home + separator + "CS1Files" + separator + "data" + separator; 68 String fullNothingElseMatters = path + NothingElseMatters; 69 return fullNothingElseMatters; 70 } 71 } 72