重定向输出流

来源:互联网 发布:科大讯飞同声翻译软件 编辑:程序博客网 时间:2024/05/17 01:12
import java.io.PrintStream;

public class Printerror {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
            try{
                PrintStream out = System.out;
                PrintStream ps = new PrintStream("./log.txt");
                System.setOut(ps);
                System.out.println("这个句子 会输出到什么地方?");
                System.setOut(out);
                System.out.println("请查看日志文件");
                
            }catch(Exception e){
                e.printStackTrace();
            }
    }

    通过setout方法,改变了system.Out的输出方式,前者的输出结果会保存到log.txt中,后者的 输出会显示在控制台中。

setErr()    public static void  setErr(PrintStream err)

setIn()      public static void setIn(InputStream in)

同理,上述两个也可以,重新设置system类的标准错误输出流,和标准输入流。因为,in,out成员变量,都是被定义成final类型的。

}
0 0
原创粉丝点击