字节缓冲流:
原理:底层自带了长度为 8192 的缓冲区。利用缓冲区可以一次读写 8192 个字节,从而提高性能
public BufferedInputStream (InputStream is) → 把基本流包装成高级流,提高读取数据的性能
public BufferedOutputStream (OutputStream os) → 把基本流包装成高级流,提高写出数据的性能
字节缓冲流拷贝文件:
1. 一次读写一个字节
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\Java基础资料\\a.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\Java基础资料\\b.txt"));
while (true) {
int b = bis.read();
if (b == -1) break;
bos.write(b);
}
bos.close();
bis.close();
2. 一次读写一个字节数组
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\Java基础资料\\a.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\Java基础资料\\b.txt"));
byte[] bytes = new byte[5];
while (true) {
int len = bis.read(bytes);
if (len == -1) break;
bos.write(bytes, 0, len);
}
bos.close();
bis.close();
标签:Java,字节,BufferedInputStream,缓冲,bos,new,bis From: https://www.cnblogs.com/gagaya2/p/17785588.html