将 System.out 转换成 PrintWriter

来源:互联网 发布:德州理工大学 知乎 编辑:程序博客网 时间:2024/05/22 14:39
System.out  是一个 PrintStream,而 PrintStream 是一个 OutputStream.。
PrintWriter 有一个可以接受 OutputStreamSystem 作为参数的构造器。因此,只要我们
需要,就可以使用那个构造器把 System.out  转换成 PrintWriter。


//: c12:ChangeSystemOut.java
// Turn System.out into a PrintWriter.
import com.bruceeckel.simpletest.*;
import java.io.*;


public class ChangeSystemOut {
    private static Test monitor = new Test();
    public static void main(String[] args) {
        PrintWriter out = new PrintWriter(System.out, true);
    out.println("Hello, world");
    monitor.expect(new String[] {
            "Hello, world" 
        }); 
    } 

} ///:~


原创粉丝点击