The following text was written to the standard output stream when the ReverseCopy program was executed from IntelliJ.
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 = "Closer.text";
String outputFileName = "CloserReversed.text";
ArrayList words = readWordsFromFile(inputFileName);
writeWordsToFile(words, outputFileName);
}
private static ArrayList readWordsFromFile(String inputFileName)
throws FileNotFoundException{
Scanner scanner = establishScanner(inputFileName);
ArrayList words = new ArrayList<>();
while (scanner.hasNext()){
String word = scanner.next();
words.add(word);
}
return words;
}
private static void writeWordsToFile(ArrayList words, String outputFileName)
throws IOException{
PrintWriter printer =getPrintWriter(outputFileName);
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;
}
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;
String fullFileName = path + fileName;
return fullFileName;
}
}