ReverseCopy.java
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;

public class ReverseCopy {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        String inputFileName = "DesolationRow.text";
        String outputFileName = "DesolationRowReversed.text";
        ArrayList<String> words = readWordsFromFile(inputFileName);
        writeWordsToFile(words,outputFileName);
    }

    private static ArrayList<String> readWordsFromFile(String inputFileName)throws FileNotFoundException{
        // Equate 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 outputFileName) throws IOException{
        // Equate a printer with an output file
        PrintWriter printer = getPrintWriter(outputFileName);
        //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 inputFileName) throws FileNotFoundException {
        String fullFileName = createFullFileName(inputFileName);
        return new Scanner(new File(fullFileName));
    }

    private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
        String fullFileName = createFullFileName(outputFileName);
//        PrintWriter printer = new PrintWriter(fullFileName);
//        return printer;
        return new PrintWriter(fullFileName);
    }
    // Create the full file name for a simple file name, assuming that it will be found in the CS1Files/data subdirectory of the user's home directory
    private static String createFullFileName(String fileName) {
        String separator = System.getProperty("file.separator");
        String home = System.getProperty("user.home");
        String path = home + separator + "CS1Files" + separator + "data" + separator;
        // Q. what if I don't use String home separately from String path, rather than include every info
        // of the directory adress in one String assignment, then it will be a valid way of coding?
        String fullFileName = path + fileName;
        // Q. Furthermore, if I write the fullFileName in one sentence, where still leaving String separator line,
        // I suppose whole method will be much simplified. Is that correct? also is that valid way of coding?
        return fullFileName;
    }
}