/*
* 輸出流體系
* ---| OutputStream? 輸出字節(jié)流的基類(lèi)是一個(gè)抽象類(lèi)
* -------| FileOutputStream 文件輸出字節(jié)流
* -------| BufferedFileOutputStream 文件輸出字節(jié)緩沖流? 內(nèi)部維護(hù)了一個(gè)8kb的字節(jié)數(shù)組,可以提高讀取效率
*
* 使用步驟:
* 1.定位目標(biāo)文件;
*? 2.構(gòu)建數(shù)據(jù)輸出通道和緩沖字節(jié)輸出流:
*? 3. 使用緩沖數(shù)據(jù)輸出流的flush刷新將緩沖區(qū)數(shù)據(jù)輸出文件:
*? 4. 關(guān)閉文件:
*
*? 說(shuō)明:當(dāng)緩沖輸出字節(jié)流的數(shù)組滿(mǎn)了,也會(huì)自動(dòng)寫(xiě)入文件:
*
*
*/
package com.michael.iodetail;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo5 {
public static void main(String[] args){
bufferedOutput();
}
public static void bufferedOutput(){
//1.定位文件
File file = new File("c:\\buf.txt");
//2. 建立文件的輸出通道
FileOutputStream fileOutputStream = null;
try{
fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bufferOutputStream = new BufferedOutputStream(fileOutputStream);
bufferOutputStream.write("Hello world!".getBytes());
bufferOutputStream.flush();
}catch(IOException e){
throw new RuntimeException(e);
}finally{
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
}
}