我对如何将控制台输出写入txt文件然后立即显示它感到困惑。任何帮助,将不胜感激。这是我到目前为止使用的代码:
public static void savedata(){
try {
PrintStream myconsole = new PrintStream(new File("Result.txt"));
System.setOut(myconsole);
myconsole.print(generateReport());
}
catch(FileNotFoundException e){
System.out.println(e);
}
}
public static void displaydata(){
File file = new File("Result.txt");
try {
Scanner input = new Scanner(file);
while (input.hasNext()){
String num = input.nextLine();
System.out.println(num);
}
}
catch(FileNotFoundException e){
System.out.println(e);
}
}