日期:2014-05-20  浏览次数:20791 次

java的输出在命令行中的重定向问题
用命令行的重定向符号>>可以输出java的System.out.println信息到文件中
但是不能输出System.err.println信息到文件中

请问有没有什么办法在命令行中将System.err.println的信息也输出到文件中去?

大家可以试试

public class TestOut {

/**
 * @param args
 */
public static void main(String[] args) {
System.out.println("out : hello");
System.out.println("out : world");
System.err.println("err : hello");
System.err.println("err : world");
}

}

------最佳解决方案--------------------
学习了 ,谢谢!
------其他解决方案--------------------
参考自
这里
------其他解决方案--------------------
执行是

java TestOut>>log.txt

只能输出
out : hello
out : world
到文件中去
err : hello
err : world
还是在窗口中输出的

------其他解决方案--------------------
自己解决了!!!


这样就可以了
java TestOut>>log.txt 2>&1
------其他解决方案--------------------
又学习,还得分,谢谢楼主啊!
------其他解决方案--------------------


public static void main(String[] args) throws FileNotFoundException {
        OutputStream out = new FileOutputStream("hello.txt");
        PrintStream ps = new PrintStream(out);
        System.setOut(ps);
        System.setErr(ps);
        System.out.println("out : hello");
        System.out.println("out : world");
        System.err.println("err : hello");
        System.err.println("err : world");
    }

------其他解决方案--------------------
引用:
Java code?123456789101112public static void main(String[] args) throws FileNotFoundException {        OutputStream out = new FileOutputStream("hello.txt");        PrintStream ps = new Pri……

当然,这样是可以的
问题是不能改程序....
------其他解决方案--------------------
引用:
又学习,还得分,谢谢楼主啊!


------其他解决方案--------------------
null