有没有办法将 Java 程序中的打印语句重定向到所需的文本文件?
Is there a way to redirect the print statements in a Java program to required text files?
如何使用 Print Stream 将 Java 程序的打印语句的内容复制到文本文件?
我从现在开始使用以下代码来执行此操作。它工作正常。可能有更好的方法。
PrintStream ps = new PrintStream("\file.txt");
PrintStream orig = System.out;
System.setOut(ps);
//TODO: stuff with System.out.println("some output");
System.setOut(orig);
ps.close();
请参阅此答案以获得更好的说明redirect to file
public static void main(String[] args) {
try {
System.setOut(new PrintStream(new File("d:/output.txt")));
System.out.println("Whosebug");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
如何使用 Print Stream 将 Java 程序的打印语句的内容复制到文本文件?
我从现在开始使用以下代码来执行此操作。它工作正常。可能有更好的方法。
PrintStream ps = new PrintStream("\file.txt");
PrintStream orig = System.out;
System.setOut(ps);
//TODO: stuff with System.out.println("some output");
System.setOut(orig);
ps.close();
请参阅此答案以获得更好的说明redirect to file
public static void main(String[] args) {
try {
System.setOut(new PrintStream(new File("d:/output.txt")));
System.out.println("Whosebug");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}