Java output console error message to file?

后端 未结 5 1912
無奈伤痛
無奈伤痛 2020-12-20 16:37

I have a simple piece of code that outputs console text to a text file in Java:

PrintStream out = new PrintStream(new FileOutputStream(\"test2_output.txt\"))         


        
相关标签:
5条回答
  • 2020-12-20 17:01

    Add:

    System.setErr(out);
    

    at the end.

    0 讨论(0)
  • 2020-12-20 17:05

    There is also a System.setErr() call to redirect stderr.

    0 讨论(0)
  • 2020-12-20 17:06

    Try:

    PrintStream pst = new PrintStream("Text.txt");  
    System.setOut(pst);
    System.setErr(pst);
    System.out.println("Hello, Finally I've printed output to file..");
    
    0 讨论(0)
  • 2020-12-20 17:10

    You're currently redirecting the standard output stream to a file. To redirect the standard error stream use

    System.setErr(out);
    
    • System.setErr()javadoc
    0 讨论(0)
  • 2020-12-20 17:11

    System.setErr(out)

    0 讨论(0)
提交回复
热议问题