一:基本概念
- 为了提高读写速度,Java API提供了带缓冲功能的流类:缓冲流
- 缓冲流要“套接”在相应的节点流之上,根据数据操作单位的不同,可以把缓冲流分为:
① 字节缓冲流:BufferedInputStream,BufferOutputStream
② 字符缓冲流:BufferedRead,BufferedWriter
- 缓冲流的基本原理:创建流对象时,内部会创建一个缓冲数组,通过缓冲区读写,减少系统IO次数,从而提高读写效率。
二:构造器
1、字节型缓冲输入输出流
- public BufferedInputStream(InputStream in):创建一个新的字节型的缓冲输入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("abc.jpg"))
- public BufferedOutputStream(OutputStream out):创建一个新的字节型的缓冲输出流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("abc_copy.jpg"))
2、字符型缓冲输入输出流
- public BufferedReader(Reader in):创建一个新的字符型的缓冲输入流
BufferedReader br = new BufferedReader(new FileReader("br.txt"))
- public BufferedWriter(Writer out):创建一个新的字符型的缓冲输出流
BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"))
三:效率测试
缓冲流的读写方法与基本的流是一致的。
以下测试代码,分别采用了FileInputStream\FileOutputStream和BufferedInputStream\BufferedOutputStream实现非文本文件的复制。
package IO.BufferStream.Efficiency;
import org.junit.Test;
import java.io.*;
public class CopyFileWithFileStreamTest {
//方法1:使用FileInputStream\FileOutputStream实现非文本文件的复制
public void copyFileWithFileStream(String srcPath, String destPath) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File(srcPath));
fos = new FileOutputStream(new File(destPath));
byte[] buffer = new byte[100];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
System.out.println("复制成功");
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
if (fis != null)
fis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Test
public void test1() {
String srcPath = "D:\\2024TianYuShiJie\\2024TianYuDataSource\\20241104资源\\test.mp4";
String destPath = "D:\\2024TianYuShiJie\\2024TianYuDataSource\\20241104资源\\test-01.mp4";
long start = System.currentTimeMillis();
copyFileWithFileStream(srcPath, destPath);
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));//花费的时间为:398
}
//方法2:使用BufferedInputStream\BufferedOutputStream实现非文本文件的复制
public void copyFileWithBufferedStream(String srcPath, String destPath) {
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File srcFile = new File(srcPath);
File destFile = new File(destPath);
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
int len;
byte[] buffer = new byte[100];
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
System.out.println("复制成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null)
bos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
if (bis != null)
bis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Test
public void test2(){
String srcPath = "D:\\2024TianYuShiJie\\2024TianYuDataSource\\20241104资源\\test.mp4";
String destPath = "D:\\2024TianYuShiJie\\2024TianYuDataSource\\20241104资源\\test-02.mp4";
long start = System.currentTimeMillis();
copyFileWithBufferedStream(srcPath,destPath);
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));//花费的时间为:35
}
}
四:字符缓冲流特有的方法
- BufferedReader: public String readLine():读一行文字;
@Test
public void testReadLine() throws IOException{
BufferedReader br = new BufferedReader(new FileReader("D:\\2024TianYuShiJie\\2024TianYuDataSource\\20241114资源\\hello.txt"));
String line;
while ((line = br.readLine()) != null){
System.out.println(line);
//I LOVE YOUyou love meso bad
//hello world this is the first hello
}
br.close();
}
- BufferedWriter: public void newLine(): 写一行行分隔符,由系统属性定义符号
@Test
public void testNewLine() throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\2024TianYuShiJie\\2024TianYuDataSource\\20241114资源\\outtest.txt"));
bw.write("中");
bw.newLine();
bw.write("间");
bw.newLine();
bw.write("人");
bw.newLine();
bw.close();
}