一、缓冲流简介
1.1 简单介绍
1、缓冲流为处理流的其中一类,分为字符型缓冲流和字节型缓冲流。
字符型缓冲流:BufferedReader、BufferedWriter
字节型缓冲流:BufferedInputStream、BufferedOutputStream
2、作用:提高流的读取、写入的速度(内部提供了一个缓冲区数组:buf)
> ① BufferedInputStream为另一个输入流添加了功能—即缓冲输入并支持标记和重置方法。
> ② 当BufferedInputStream被创建时,会创建一个内部的缓冲区数组(8192 byte)。
> ③ 当读取或跳过流中的字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,每次填充多个字节。(待探索)
1.2 继承关系以及整体框架
// BufferedInputStream 类声明,继承于FilterInputStream
public class BufferedInputStream extends FilterInputStream
// BufferedOutputStream 类声明,继承于FileOutputStream
public class BufferedOutputStream extends FilterOutputStream
|--java.lang.Object
|--java.io.InputStream
|--java.io.FilterInputStream
|--java.io.BufferedInputStream
|--java.io.OutputStream
|--java.io.FilterOutputStream
|--java.io.FilterOutputStream
// BufferedReader 类声明,继承于Reader
public class BufferedReader extends Reader
// BufferedWriter 类声明,继承于Writer
public class BufferedWriter extends Writer
|--java.lang.Object
|--java.io.Reader
|--java.io.BufferedReader
|--java.io.Writer
|--java.io.BufferedWriter
二、缓冲流的使用示例
2.1 使用缓冲流进行视频复制和未使用缓冲流进行视频复制的效率对比
// 使用缓冲流复制视频
/*
方法需要两个参数:
参数1:String类型,需要复制的视频的绝对路径
参数2:String类型,复制的视频存放位置(包含视频文件名称)的绝对路径
*/
public static void copyVideo(String srcPath, String destPath){
// 声明输入缓冲流和输出缓冲流,初始化赋值为null
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 1. 创建文件对象,分别创建源视频文件和目标视频文件的File对象
File inputFile = new File(srcPath);
File outputFile = new File(destPath);
// 2. 创建节点流对象(需要使用字节流)和缓冲流对象
// 2.1 创建节点流对象,并连接绑定源视频File对象和目标视频File对象
FileInputStream fileInputStream = new FileInputStream(inputFile);
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
// 2.2 创建缓冲流对象,并分别连接绑定输入节点流对象和输出节点流对象
bis = new BufferedInputStream(fileInputStream);
bos = new BufferedOutputStream(fileOutputStream);
// 3. 读取和写入操作
byte[] b = new byte[1024]; // 设置每次读取输入流字节数为1024字节
int readLength;
while ((readLength = bis.read(b)) != -1) {
bos.write(b, 0, readLength);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4. 关闭流操作
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 结果说明:使用缓冲流复制视频文件花费时间:857毫秒(此时间根据电脑性能不同时间不一致,视频文件大小229MB)
// 未使用缓冲处理流
public static void copyVideoV2(String srcPath, String destPath) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File inputFile = new File(srcPath);
File outputFile = new File(destPath);
fis = new FileInputStream(inputFile);
fos = new FileOutputStream(outputFile);
byte[] b = new byte[1024];
int readLength;
while ((readLength = fis.read(b)) != -1) {
fos.write(b, 0, readLength);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 结果说明 未使用缓冲流复制视频文件花费时间:4880秒(时间相差5+倍)
2.1 使用缓冲流进行文本文件复制和未使用缓冲流进行文本复制代码
说明:由于我电脑的文本文件基本比较小,因此文本复制使用和不使用缓冲流对比差别不大。(可以自己造大的文本文件进行测试)
// 使用缓冲流
// 缓冲流(字符型)复制文本文件方法
public static void copyTextFile(String srcPath, String destPath) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
// 输入和输出的文件对象
File inputFile = new File(srcPath);
File outputFile = new File(destPath);
// 输入输出节点流
FileReader fr = new FileReader(inputFile);
FileWriter fw = new FileWriter(outputFile);
// 缓冲流
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
// 读取和写入文件
char[] cbuf = new char[1024];
int readLength;
while ((readLength = br.read(cbuf)) != -1) {
bw.write(cbuf, 0, readLength);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭缓冲流(关闭缓冲流的同时会关闭节点流)
if (br != null)
// 关闭缓冲流(关闭缓冲流的同时会关闭节点流)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if (bw != null)
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 未使用缓冲流赋值文本文件
public static void copyTextFileV2(String srcPath, String destPath) {
FileReader fr = null;
FileWriter fw = null;
try {
// 输入和输出的文件对象
File inputFile = new File(srcPath);
File outputFile = new File(destPath);
// 输入输出节点流
fr = new FileReader(inputFile);
fw = new FileWriter(outputFile);
// 读取和写入文件
char[] cbuf = new char[255];
int readLength;
while ((readLength = fr.read(cbuf)) != -1) {
fw.write(cbuf, 0, readLength);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭缓冲流(关闭缓冲流的同时会关闭节点流)
if (fr != null)
// 关闭缓冲流(关闭缓冲流的同时会关闭节点流)
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
if (fw != null)
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}