package arraylistplay; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; import static arrayplay.ReverseCopy.createFullFileName; public class ReverseCopy { public static void main(String[] args) throws FileNotFoundException, IOException { String inputFileName = "POWER"; String outputFileName = "POWERReversed"; ArrayList<String> words = readWordsFromFile(inputFileName); writeWordsToFile(words, outputFileName); } private static ArrayList<String> readWordsFromFile(String inputFileName) throws FileNotFoundException { // Add a scanner with the input file Scanner scanner = establishScanner(inputFileName); // Read the words from the file into a dynamically growing ArrayList ArrayList<String> words = new ArrayList<>(); while (scanner.hasNext()) { String word = scanner.next(); words.add(word); } // Return the words return words; } private static void writeWordsToFile(ArrayList<String> words, String POWERReversed) throws IOException { // Equate a printer with an output file PrintWriter printer = getPrintWriter(POWERReversed); // Print the words to the file for (int x = words.size() - 1; x >= 0; x = x - 1) { printer.println(words.get(x)); } printer.close(); } private static Scanner establishScanner(String POWER) throws FileNotFoundException { String fullFileName = createFullFileName(POWER); return new Scanner(new File(fullFileName)); } private static PrintWriter getPrintWriter(String POWERReversed) throws FileNotFoundException { String fullFileName = createFullFileName(POWERReversed); PrintWriter printer = new PrintWriter(fullFileName); return printer; }}