缓冲流也叫高效流,是处理流的一种,即是作用在流上的流。其目的就是加快读取和写入数据的速度。

缓冲流本身并没有IO功能,只是在别的流上加上缓冲效果从而提高了效率。当对文件或其他目标频繁读写或操作效率低,效能差时。这时使用缓冲流能够更高效的读写信息。因为缓冲流先将数据缓存起来,然后一起写入或读取出来。所以说,缓冲流还是很重要的,在IO操作时加上缓冲流提升性能。

Java IO流中对应的缓冲流有以下四个:

字节缓冲流:BufferedInputStream、BufferedOutputStream

字符缓冲流:BufferedReader、BufferedWriter

1、字节缓冲流

构造方法:

输入流:

  • BufferedInputStream(InputStream in):创建一个新的字节缓冲输入流,传入的参数是InputStream类型,缓冲区默认大小为8129。
  • BufferedInputStream(InputStream in, int size):创建一个指定缓冲区大小的字节缓冲输入流。

输出流:

  • BufferedOutputStream(OutputStream out):创建一个新的字节缓冲输出流,传入的参数是OutputStream ,以将数据写入指定的基础输出流。
  • BufferedOutputStream(OutputStream out, int size):创建一个指定缓冲区大小的的字节缓冲输出流,以将具有指定缓冲区大小的数据写入指定的基础输出流。

构造方法举例代码如下:

    //字节缓冲输入输出流,此种方法默认缓冲区大小8192
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\IO\\hello.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\IO\\hello.txt"));
//自定义缓冲区大小
BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream("D:\\IO\\hello.txt"),10240);
BufferedOutputStream bos1 = new BufferedOutputStream(new FileOutputStream("D:\\IO\\hello.txt"),10240);

前面就说缓冲流可以加快读取和写入数据的速度,所以现在就来比较一下使用普通流和使用缓冲流的效率对比(拷贝一个886MB大小的视频):

普通流代码示例:

package com.thr;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; /**
* @author Administrator
* @date 2020-02-26
* @desc 普通流测试
*/
public class BufferedDemo {
public static void main(String[] args) {
//定义流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//开始时间
long start = System.currentTimeMillis();
//创建流对象
fis = new FileInputStream("D:\\IO\\1.mp4");
fos = new FileOutputStream("D:\\IO\\2.mp4");
//读写操作
int len;
byte[] buffer = new byte[1024];
while ((len=fis.read(buffer))!=-1){
fos.write(buffer,0,len);
}
//结束时间
long end = System.currentTimeMillis();
System.out.println("完成,共耗时:"+(end-start)+"ms"); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//完成,共耗时:5165ms

缓冲流代码示例:

package com.thr;

import java.io.*;

/**
* @author Administrator
* @date 2020-02-26
* @desc 缓冲流测试
*/
public class BufferedDemo1 {
public static void main(String[] args) {
//定义缓冲流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//开始时间
long start = System.currentTimeMillis();
//创建缓冲流对象,注意参数传的FileXXX,而不文件目录
bis = new BufferedInputStream(new FileInputStream("D:\\IO\\1.mp4"));
bos = new BufferedOutputStream(new FileOutputStream("D:\\IO\\3.mp4"));
//读写操作
int len;
byte[] buffer = new byte[1024];
while ((len=bis.read(buffer))!=-1){
bos.write(buffer,0,len);
}
//结束时间
long end = System.currentTimeMillis();
System.out.println("完成,共耗时:"+(end-start)+"ms"); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bos.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//完成,共耗时:1658ms

我们可以看出缓冲流大概只用了三分之一的时间就完成了同样的工作。

2、字符缓冲流

构造方法

输入流:

  • BufferedReader(Reader in):创建一个新的字符缓冲输入流,传入的参数是Reader类型,缓冲区默认大小为8129。
  • BufferedReader(Reader in, int sz):创建一个指定大小缓冲区的字符缓冲输入流。

输出流:

  • BufferedWriter(Writer out):创建一个新的字符缓冲输出流,传入的参数是Writer类型,缓冲区默认大小为8129。
  • BufferedWriter(Writer out, int sz):创建一个指定大小缓冲区的字符缓冲输出流。

构造方法举例代码如下:

    //字符缓冲输入输出流,此种方法默认缓冲区大小为defaultCharBufferSize = 8192;
BufferedReader br = new BufferedReader(new FileReader("D:\\IO\\hello.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\IO\\hello.txt"));
//自定义缓冲区大小
BufferedReader br1 = new BufferedReader(new FileReader("D:\\IO\\hello.txt"),10240);
BufferedWriter bw1 = new BufferedWriter(new FileWriter("D:\\IO\\hello.txt"),10240);

字符缓冲流和字节缓冲流的使用大致一样,只是两者处理的东西不一样。但是在字符缓冲流中它有两个独特的方法。

  • BufferedReader:public String readLine():读一行数据。 读取到最后返回null。
  • BufferedWriter:public void newLine():换行,该方法内部调用了lineSeparator,它表示的换行符。

两个方法使用举例:

package com.thr;

import java.io.*;

/**
* @author Administrator
* @date 2020-02-26
* @desc ReadLine和newLine的使用
*/
public class BufferedTest {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
try { br = new BufferedReader(new FileReader("D:\\IO\\hello.txt"));
bw = new BufferedWriter(new FileWriter("D:\\IO\\hi.txt")); //1、普通字符数组方式
/* int len;
char[] buffer = new char[1024];
while ((len=br.read(buffer))!=-1){
bw.write(buffer,0,len);
}
System.out.println("拷贝完成...");*/ //2、使用readLine和newLine的方式
String data;
while ((data=br.readLine())!=null){//不再是-1,因为返回的String类型
//每次读取一行数据
bw.write(data);//这样输出来的文件是没有换行的,所以要在后面加上newLine()方法用来换行
bw.newLine();
}
System.out.println("拷贝完成..."); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

最新文章

  1. 关于打印机能PING通但是无法打开\\地址的问题
  2. ABAP 内表的行列转换
  3. innodb insert buffer 插入缓冲区的理解
  4. 命令行导入SQL文件
  5. 《GK101任意波发生器》升级固件发布(版本:1.0.2build539)
  6. Windows下配置使用WinPcap
  7. BZOJ 3198 SDOI2013 spring
  8. Linux vmstat:报告虚拟内存统计的工具
  9. 小学生玩ACM----广搜
  10. 分享php中四种webservice实现的简单架构方法及实例(转)
  11. 【翻译自mos文章】改变数据库用户sysman(该用户是DB Control Repository 的schema)password的方法
  12. 使用python操作InfluxDB
  13. jQuery操作之效果
  14. 第十八章 DjangoWeb开发框架
  15. PS图层混合算法之三(滤色, 叠加, 柔光, 强光)
  16. 还原堆栈信息,分析地形系统使用ASTC格式的纹理导致Crash的问题
  17. 安装Xampp-配置appche,mysql运行环境遇到的坑(转)
  18. wireshark----linux
  19. php的哈希函数
  20. linux 常用命令总结(一)

热门文章

  1. hadoop之mr框架的源码理解注意点
  2. MyBatis(6)——分页的实现
  3. JS高级---为内置对象添加原型方法
  4. Abp中打开错误信息输出
  5. html()和append()
  6. 《深入理解Java虚拟机》读书笔记八
  7. 吴裕雄 python 机器学习——支持向量机线性分类LinearSVC模型
  8. 题解【SP1043】 GSS1 - Can you answer these queries I
  9. 记一道简单的re--BUUctf reverse1
  10. power-plan如何定