字节缓冲流默认缓冲空间是8k,提升了性能
最底层是节点流类1,释放的过程是由内向外释放的,因此直接释放字节流就行
如果要手动全部释放的话,就需要从里到外依次释放
节点流类:用于直接操作目标设备所对应的流类。节点流类所对应的IO源或目标称为流节点。比如我们用一个类和一个文件或网络相关联,那么这个类就叫做节点流类,这个文件或网络就叫做流的节点。
BufferedInputStream
继承关系
![](https://i-blog.csdnimg.cn/blog_migrate/c74a29a5d0b7af3bc043ed55b7f87d72.png)
构造方法
Constructor | Description |
---|---|
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 Type | Description |
---|---|
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
继承关系
![](https://i-blog.csdnimg.cn/blog_migrate/890e3533e6e80a0c0bd24e4d6b9cb153.png)
构造方法
Constructor | Description |
---|---|
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 Type | Description |
---|---|
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();
}
}
}
}
FileInputStream和FileOutputStream
↩︎