120.字节缓冲流(BufferedInputStream&BufferedOutputStream)

字节缓冲流默认缓冲空间是8k,提升了性能

最底层是节点流类1,释放的过程是由内向外释放的,因此直接释放字节流就行

如果要手动全部释放的话,就需要从里到外依次释放

节点流类:用于直接操作目标设备所对应的流类。节点流类所对应的IO源或目标称为流节点。比如我们用一个类和一个文件或网络相关联,那么这个类就叫做节点流类,这个文件或网络就叫做流的节点。

BufferedInputStream
继承关系
构造方法
ConstructorDescription
BufferedInputStream(InputStream in)Creates a BufferedInputStream and saves its argument, the input stream in, for later use.
BufferedInputStream(InputStream in, int size)Creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use.
接口方法
Method Modifier and TypeDescription
int available()Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
void close()Closes this input stream and releases any system resources associated with the stream.
void mark(int readlimit)See the general contract of the mark method of InputStream.
boolean markSupported()Tests if this input stream supports the mark and reset methods.
int read()See the general contract of the read method of InputStream.
int read(byte[] b, int off, int len)Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.
void reset()See the general contract of the reset method of InputStream.
long skip(long n)See the general contract of the skip method of InputStream.
测试BufferedInputStream
package cn.yzy.io;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class testBufferedInputStream {
	public static void main(String[] args) {
		//创建源
		File srcFile = new File("test.txt");
		
		//选择流
		InputStream is = null;
		
		//缓冲字节流
		BufferedInputStream bis = null;
		try {
			is = new FileInputStream(srcFile); //FileInputStream是通知操作系统取释放资源的
			bis = new BufferedInputStream(is); //BufferedInputStream是由虚拟机的垃圾回收机制释放资源的
			//按段读取
			byte[] flush = new byte[1024]; //缓冲容器
			int len = 0;
			while((len=bis.read(flush))!=-1) {
				String str = new String(flush, 0, len);
				System.out.println(str);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//释放InputStream(可以不用释放)
			try {
				if(null != is)
					is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			//释放BufferedInputStream(close的过程是逐层释放的)
			try {
				if(null != bis)
					bis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
}

以上代码实际可以这样嵌套着写:

is = new BufferedInputStream(new FileInputStream(srcFile));

BufferedOutputStream
继承关系
构造方法
ConstructorDescription
BufferedOutputStream(OutputStream out)Creates a new buffered output stream to write data to the specified underlying output stream.
BufferedOutputStream(OutputStream out, int size)Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.
接口方法
Method Modifier and TypeDescription
void flush()Flushes this buffered output stream.
void write(byte[] b, int off, int len)Writes len bytes from the specified byte array starting at offset off to this buffered output stream.
void write(int b)Writes the specified byte to this buffered output stream.
测试BufferedOutputStream
package cn.yzy.io;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class testBufferedOutputStream {
	public static void main(String[] args) {
		//创建源
		File srcFile = new File("test.txt");
		
		//选择流
		OutputStream os = null;
		try {
			os = new BufferedOutputStream(new FileOutputStream(srcFile));
			//写
			String msgString = "IO is easy!";
			byte[] datas = msgString.getBytes(); //字符串->字节数组
			os.write(datas, 0, datas.length);
			os.flush();
			System.out.println(datas.length);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//释放InputStream(可以不用释放)
			try {
				if(null != os)
					os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

  1. FileInputStream和FileOutputStream ↩︎

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值