[英]Is there a way to redirect the print statements in a Java program to required text files?
如何使用Print Stream将Java程序的print语句的内容复制到文本文件?
我现在使用下面的代码来执行此操作。 工作正常。 可能有更好的方法。
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();
咨询此答案以更好地说明重定向到文件
public static void main(String[] args) {
try {
System.setOut(new PrintStream(new File("d:/output.txt")));
System.out.println("StackOverflow");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.