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"
});
}
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"
});
}
} ///:~