同时将控制台输出写入文件和控制台

write console output in a file and console at the same time

我想知道是否可以让 java 将所有控制台输出写入 txt 文件而不在控制台上禁用它

PrintStream o = new PrintStream(new File("A.txt"));
PrintStream console = System.out;
System.setOut(o);
System.setOut(console);

所以我需要执行这两个命令

由于我的名声,我无法发表评论。但下面的答案将解决您的问题: