緩沖字節流
默認有8192字節的緩沖區
package com.wkcto.chapter06.filterstream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 字節緩沖流
* BufferedInputStream/BufferedOutputStream
* @author 蛙課網
*
*/
public class Test01 {
public static void main(String[] args) throws IOException {
//1)使用字節緩沖流讀取文件內容
// readData();
//2)使用字節緩沖流保存數據到文件
writeData();
}
//使用字節緩沖流保存數據到文件
private static void writeData() throws IOException {
//在當前程序與指定的文件間建立字節流通道
OutputStream out = new FileOutputStream("d:/def.txt");
//使用字節緩沖流對out字節流進行包裝(緩沖)
BufferedOutputStream bos = new BufferedOutputStream(out);
//使用緩沖流保存數據, 現在是把數據保存到緩沖流的緩沖區中
bos.write(97);
byte[] bytes = "wkcto is a good websit".getBytes();
bos.write(bytes);
// bos.flush(); //把緩沖區的數據清空到文件里
bos.close();
}
//使用字節緩沖流讀取文件內容
private static void readData() throws IOException {
//在當前程序與指定的文件之間建立字節 流通道
InputStream in = new FileInputStream("d:/abc.txt");
//對字節流進行緩沖
BufferedInputStream bis = new BufferedInputStream(in);
//使用緩沖字節流讀取文件內容
int cc = bis.read();
while( cc != -1){
System.out.print( (char)cc );
cc = bis.read();
}
bis.close(); //關閉緩沖流, 也會把被包裝的字節流關閉
}
}