打印流的代碼
import java.io.BufferedOutputStream;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
/*
?* 打印流
?* PrintStream
?*/
public class PrintTest01 {
?? ?public static void main(String[] asrgs) throws FileNotFoundException {
?? ??? ?
?? ??? ?System.out.println("你好");
?? ??? ?PrintStream ps=System.out;
?? ??? ?ps.println("我希望世界和平");
?? ??? ?ps=new PrintStream(new BufferedOutputStream(new FileOutputStream("print.txt")));
?? ??? ?//可以丟文件可以丟路徑還可以丟字節(jié)流
?? ??? ?ps.println("中國加油");
?? ??? ?ps.flush();
?? ??? ?ps.close();
?? ??? ?
?? ??? ?//重定向輸出端
?? ??? ?System.setOut(ps);
?? ??? ?System.out.println("已經(jīng)不會輸出到控制臺了");
?? ??? ?//重定向回到控制臺
?? ??? ?System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true));
?? ??? ?System.out.println("我回來了");
?? ??? ?
?? ?}
}
標(biāo)簽: