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
* 字節(jié)打印流
* @author 蛙課網(wǎng)
*
*/
public class Test03 {
public static void main(String[] args) throws FileNotFoundException {
//在追加的方式建立與文件的字節(jié)流通道
OutputStream out = new FileOutputStream("d:/log.txt", true);
//創(chuàng)建打印流
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代表系統(tǒng)的標(biāo)準(zhǔn)輸出設(shè)備,顯示器,");
//修改System.out的打印輸出方向
System.setOut(pStream);
System.out.println("現(xiàn)在打印的信息就不是顯示在屏幕上了, 而是打印到pstream流中,即log.txt文件中");
//有時, 也會把異常信息打印到日志文件中
try {
FileInputStream fis = new FileInputStream("F:/abc.txt");
} catch (Exception e) {
// 在開發(fā)時,一般是把異常打印到屏幕上,方便程序員調(diào)試
// e.printStackTrace();
// 在部署后, 經(jīng)常把異常打印到日志文件中
e.printStackTrace(pStream);
}
pStream.close();
}
}
裝飾者設(shè)計(jì)模式
設(shè)計(jì)模式就是別人總結(jié)的一套解決方案, 這套解決方案被大多數(shù)人熟知與認(rèn)可
裝飾者設(shè)計(jì)模式是對現(xiàn)有類的現(xiàn)有方法進(jìn)行功能的擴(kuò)展
在IO流相關(guān)類中,以Filter開頭的類采用了裝飾者設(shè)計(jì)模式