当前位置:   article > 正文

120.字节缓冲流(BufferedInputStream&BufferedOutputStream)_buffer = new byte[32768];字节缓冲给多大

buffer = new byte[32768];字节缓冲给多大

字节缓冲流默认缓冲空间是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();
			}
		}
		
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

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

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

  1. FileInputStream和FileOutputStream ↩︎

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/146985
推荐阅读
相关标签
  

闽ICP备14008679号