java IO流之BufferedInputStream、 BufferedOutputStream缓冲字节流
对比几种字节流访问方式
使用缓冲区字节流能减少访问存储器的次数,加快运行速度,延长存储器使用年限
想要完成缓冲区的效果,单纯的靠File-In/Out-putStream是不能完成的,这个时候就需要引入新的流(在File-In/Out-putStream外面再套一层流):BufferedInputStream ,BufferedOutputStream。
BufferedInputStream
字段摘要
- protected byte[] buf 存储数据的内部缓冲区数组。
- protected int count 比缓冲区中最后一个有效字节的索引大 1 的索引。
- protected int marklimit 调用 mark 方法后,在后续调用 reset 方法失败之前所允许的最大提前读取量。
- protected int markpos 最后一次调用 mark 方法时 pos 字段的值。
- protected int pos 缓冲区中的当前位置。
构造方法摘要
- BufferedInputStream(InputStream in)
- 创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
- BufferedInputStream(InputStream in, int size)
- 创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
方法
Modifier and Type | Method | Description |
int | available() | 返回可以从此输入流读取(或跳过)、且不受此输入流接下来的方法调用阻塞的估计字节数。 |
void | close() | 关闭此输入流并释放与该流关联的所有系统资源。 |
abstract int | read() | 从输入流中读取数据的下一个字节。 |
int | read(byte[] b) | 从此输入流中将 byte.length 个字节的数据读入一个 byte 数组中。如果因为已经到达流末尾而没有更多的数据,则返回 -1。 |
int | read(byte[] b, int off, int len) | 从此字节输入流中给定偏移量处开始将各字节读取到指定的 byte 数组中。 |
boolean | markSupported() | 测试此输入流是否支持 mark 和 reset 方法。 |
void | mark(int readlimit) | 在此输入流中标记当前的位置,readlimit - 在标记位置失效前可以读取字节的最大限制。 |
void | reset() | 将此流重新定位到最后一次对此输入流调用 mark 方法时的位置,以便后续读取重新读取相同的字节。 。 |
long | skip(long n) | 跳过和丢弃此输入流中数据的 n 个字节。 |
BufferedOutputStream
字段摘要
- protected byte[] buf 存储数据的内部缓冲区。
- protected int count 缓冲区中的有效字节数。
构造方法摘要
- BufferedOutputStream(OutputStream out)
- 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
- BufferedOutputStream(OutputStream out, int size)
- 创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。
方法
Modifier and Type | Method | Description |
void | flush() | 刷新此缓冲的输出流。关于 bos.flush(),底层调用flushBuffer()已完成刷新缓冲区的操作,无需手动完成。 |
void | write(byte[] b, int off, int len) | 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此缓冲的输出流。 |
void | write(int b) | 将指定的字节写入此缓冲的输出流。 |
- 从类 java.io.FilterOutputStream 继承的方法
close, write
分布实现
以复制文件为例
- File类,创建源文件与目标文件对象
//1.1源图片文件
File f1 = new File("IOStream/src/testfile/test.txt");
//1.2目标文件
File f2 = new File("IOStream/src/testfile/demo.txt");
- File-In/Out-putStream创建字节流对象
//2.1有一个输入的管道FileInputStream怼到源文件:
FileInputStream fis = new FileInputStream(f1);
//2.2有一个输出的管道FileOutputStream怼到目标文件上:
FileOutputStream fos = new FileOutputStream(f2);
- Buffered-In/Out-putStream类,功能加强,创建缓冲区对象
//3.1功能加强,在FileInputStream外面套一个管:BufferedInputStream:
BufferedInputStream bis = new BufferedInputStream(fis);
//3.2功能加强,在FileOutputStream外面套一个管:BufferedOutputStream:
BufferedOutputStream bos = new BufferedOutputStream(fos);
- 内容操作
//4.开始动作 :
byte[] b = new byte[1024 * 6];
int len = bis.read(b);
while (len != -1) {
bos.write(b, 0, len);
len = bis.read(b);
}
- 关闭流,倒着关闭,如果处理流包裹着节点流的话,那么其实只要关闭高级流(处理流),那么里面的字节流也会随之被关闭。
bos.close();
bis.close();
综合程序
使用try-catch-finally
public class IoStreamTC {
public static void main(String[] args){
//1.源图片文件目标文件创建实例
File f1 = new File("IOStream/src/testfile/test.txt");
File f2 = new File("IOStream/src/testfile/demo.txt");
//2.File-In/Out-putStream创建字节流对象
FileInputStream fis = null;
FileOutputStream fos = null;
//3.功能加强,在File-In/Out-putStream外面套一个管:Buffered-In/Out-putStream
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(f1);
fos = new FileOutputStream(f2);
//3.功能加强,在File-In/Out-putStream外面套一个管:Buffered-In/Out-putStream
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//4.复制文件内容
byte[] b = new byte[1024 * 6];
int len = bis.read(b);
while (len != -1) {
bos.write(b, 0, len);
len = bis.read(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("找不到文件");
} catch (IOException e) {
e.printStackTrace();
}finally{
//5.关闭流:倒着关,只要关闭高级流(处理流),字节流会随之被关闭。
try {
assert bos != null;
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
assert bis != null;
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
使用try-with-resource
public class IoStreamTC {
public static void main(String[] args){
//1.源图片文件目标文件创建实例
File f1 = new File("IOStream/src/testfile/p1.png");
File f2 = new File("IOStream/src/testfile/demo.txt");
//2.File-In/Out-putStream创建字节流对象
//3.功能加强,在File-In/Out-putStream外面套一个管:Buffered-In/Out-putStream
try (
FileInputStream fis = new FileInputStream(f1);
FileOutputStream fos = new FileOutputStream(f2);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
){
//4.复制文件内容
long startTime = System.currentTimeMillis();//记录开始时间
byte[] b = new byte[1024 * 6];
int len = bis.read(b);
while (len != -1) {
bos.write(b, 0, len);
len = bis.read(b);
}
long endTime = System.currentTimeMillis();
System.out.println("复制完成的时间为:"+(endTime-startTime));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("找不到文件");
} catch (IOException e) {
e.printStackTrace();
}
}
}