I'm new to Java, so apologies if any of this is unclear - I want to print out what is on the console to a text file also - however I only want it to print to the file at the end - so basically it will run through all the programs (which is a guessing game), and then once complete it will ask the user to enter a file name and then print what is on the console to that file. I'm somewhat familiar with PrintWriter and BufferedWriter etc but I'm not sure what to do when I want to print out results that exist on the console such as below (where the questions are system.out.print and the digits 3, 18, 19 are user input). Any help would be much appreciated!
Please guess a number between 1 and 20: 3
Too low, try again. Please guess a number between 1 and 20: 18
Too low, try again. Please guess a number between 1 and 20: 19
Perfect!
System.out.println("Enter a file name: ");
String fileName = Keyboard.readInput();
File myFile = new File(fileName);
try (PrintWriter pw = new PrintWriter(new FileWriter(fileName))) {
pw.write(??????);
} catch (IOException e) {
System.out.println(e.getMessage());
}