重定向输出流练习 ToLog

public class ToLog {

    public static void main(String[] args) throws Exception {
        //准备日志文件
        File f = new File("log.txt");
        if(!f.exists()){
            f.createNewFile();
        }
        
        
        //设置输出流
        PrintStream out = System.out;
        PrintStream ps = new PrintStream(f);
        System.setOut(ps);
        
        //写入数据
        String s = "This is a test sentence.";
        System.out.println(s);
        
        //复原输出流
        System.setOut(out);
        ps.close();
    }

}

 

posted @ 2014-04-29 20:25  mycome  阅读(219)  评论(0编辑  收藏  举报