package arrayplay; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class ReverseCopy { public static void main(String[] args) throws FileNotFoundException, IOException { String inputFileName = "ForeverYoung.text"; String outputFileName = "ForeverYoungReversed.text"; String[] words = readWordsFromFile(inputFileName); writeWordsToFile(words,outputFileName); } private static final int LIMIT = 1000; private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException{ // Equate a scanner with the input file Scanner scanner = establishScanner(inputFileName); // Read the words from the file into an oversized array String[] temp = new String[LIMIT]; int index = 0; while(scanner.hasNext()){ String word = scanner.next(); temp[index] = word; index = index +1; } int wordCount = index; //Transfer the words to a perfectly sized array String[] words = new String[wordCount]; for( int x = 0; x < wordCount; x = x+1){ words[x] = temp[x]; } //Return the words return words; } private static void writeWordsToFile(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.length - 1; x >= 0; x = x -1){ printer.println(words[x]); // Q. What's the difference btw/ xxxx.out.println( ...); vs xxxx.println(...); // Q-1.what happens if .out. is dismissed? } printer.close(); //Q. What the .close mean? how does it work in Java and especially in this code } private static Scanner establishScanner(String inputFileName) throws FileNotFoundException{ String fullFileName = createFullFileName(inputFileName); return new Scanner(new File(fullFileName)); // how the return can be in a type: new? And also what the Scanner(new File(...)); // why it is possible that new Scanner, which has just been made, can have new Files in its parameter? // And how all operators in the line including return works? } private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException{ String fullFileName = createFullFileName(outputFileName); PrintWriter printer = new PrintWriter(fullFileName); //Q. What's PrintWriter in Java? how does it work and why it is useful compared to using sout instead? return printer; } // 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) /* Q. why the throws isn't here? */ { 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; } }