Java面向对象
Java异常
Java数组
Java常用类
Java集合
Java IO流
Java线程
Java反射
Socket编程
Java注解开发
Java GoF设计模式
HashMap
Java内存模型
Java线性表

Java缓冲输入输出流

缓冲字节流

默认有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(); 	//关闭缓冲流, 也会把被包装的字节流关闭
	}

}

 

全部教程