PrintStream
package com.wkcto.chapter06.filterstream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
/**
* PrintStream
* 字節打印流
* @author 蛙課網
*
*/
public class Test03 {
public static void main(String[] args) throws FileNotFoundException {
//在追加的方式建立與文件的字節流通道
OutputStream out = new FileOutputStream("d:/log.txt", true);
//創建打印流
PrintStream pStream = new PrintStream(out);
pStream.print("hello"); //打印,不換行
pStream.println(" wkcto"); //打印,換行
pStream.println("feifei");
System.out.println("在屏幕上打印信息, System類的out成員就是一個PrintStream打印流");
System.out.println("System.out代表系統的標準輸出設備,顯示器,");
//修改System.out的打印輸出方向
System.setOut(pStream);
System.out.println("現在打印的信息就不是顯示在屏幕上了, 而是打印到pstream流中,即log.txt文件中");
//有時, 也會把異常信息打印到日志文件中
try {
FileInputStream fis = new FileInputStream("F:/abc.txt");
} catch (Exception e) {
// 在開發時,一般是把異常打印到屏幕上,方便程序員調試
// e.printStackTrace();
// 在部署后, 經常把異常打印到日志文件中
e.printStackTrace(pStream);
}
pStream.close();
}
}
裝飾者設計模式
設計模式就是別人總結的一套解決方案, 這套解決方案被大多數人熟知與認可
裝飾者設計模式是對現有類的現有方法進行功能的擴展
在IO流相關類中,以Filter開頭的類采用了裝飾者設計模式