java控制台内容输出到文件

PrintStream ps = new PrintStream("E:\auditlog.txt");
System.setOut(ps);
System.out.println(str);
我这段代码为什么不能让我的输出输出到指定文件,控制台确实没东西了,但是程序运行完文件还是空的

使用outputstream进行输出

 import java.io.PrintStream;
public class Demo02 {
    public static void main(String[] args) {
        try{
            String str = "nihao";
            PrintStream ps = new PrintStream("E:\\auditlog.txt");
            System.setOut(ps);
            System.out.println(str);
        }
        catch(Exception e){
        }

    }
}

兄弟你的程序是对的啊,我不知道你为什么错了,所以我把完整程序贴上来了你自己运行吧!

图片说明