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

缓冲流本身并没有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();
}
}
}
}
}

Java IO流详解(五)——缓冲流的更多相关文章

  1. JAVA IO 类库详解

    JAVA IO类库详解 一.InputStream类 1.表示字节输入流的所有类的超类,是一个抽象类. 2.类的方法 方法 参数 功能详述 InputStream 构造方法 available 如果用 ...

  2. Java IO最详解

    初学java,一直搞不懂java里面的io关系,在网上找了很多大多都是给个结构图草草描述也看的不是很懂.而且没有结合到java7 的最新技术,所以自己来整理一下,有错的话请指正,也希望大家提出宝贵意见 ...

  3. Mac下Intellij IDea发布Java Web项目详解五 开始测试

    测试前准备工作目录 Mac下Intellij IDea发布Web项目详解一 Mac下Intellij IDea发布Java Web项目(适合第一次配置Tomcat的家伙们)详解二 Mac下Intell ...

  4. Java - IO System类支持和缓冲流

    System类的支持和缓冲流 System类对IO的支持 在System类中,为了支持IO操作提供了三个常量: 错误输出: public static final PrintStream err; 输 ...

  5. IO流详解

    目录 IO流 IO流概述及其分类 IO概念 流按流向分为两种: 流按操作类型分为两种: 常用的IO流类 字节流的抽象父类: 字符流的抽象父类: InputStream & FileInputS ...

  6. 基于JavaSE阶段的IO流详解

    1.IO流基本概述 在Java语言中定义了许多针对不同的传输方式,最基本的就是输入输出流(俗称IO流),IO流是属于java.io包下的内容,在JavaSE阶段主要学下图所示的: 其中从图中可知,所有 ...

  7. Java基础-IO流对象之字符缓冲流(BufferedWriter与BufferedReader)

    Java基础-IO流对象之字符缓冲流(BufferedWriter与BufferedReader) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.字符缓冲流 字符缓冲流根据流的 ...

  8. Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream)

    Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在我们学习字 ...

  9. java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET

    java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了!      社区福利快来领取免费参加MDCC大会机会哦    Tag功能介绍—我们 ...

随机推荐

  1. Django_第三方

    1. 验证码 2. 绘制过程 加一个随机数用于更新验证码 不加,图片的src不会更新,图片也不会更新 加了,url也能匹配上,去执行视图函数 3. 富文本 就是带着样式的文本 3.1 使用

  2. Customized Mini LED Keychain For Better Brand Identity

    Looking for products that tell people the brand name? Then you'll find an affordable product that wi ...

  3. Python基础知识详解 从入门到精通(七)类与对象

    本篇主要是介绍python,内容可先看目录其他基础知识详解,欢迎查看本人的其他文章Python基础知识详解 从入门到精通(一)介绍Python基础知识详解 从入门到精通(二)基础Python基础知识详 ...

  4. gRPC in ASP.NET Core 3.x -- Protocol Buffer(3)更新消息类型

    当你第一次定义Protocol Buffer的消息的时候,你肯定会给消息设定一套规则需求.但是随着时间的推进,你的业务可能会发生了变化,与此同时,你的Protocol Buffer消息类型的需求也会随 ...

  5. CentOS 7 下挂载NTFS盘及开机自动挂载

    一.工具 NTFS-3G 二.安装2种安装方式 2.1.yum安装 yum install NTFS* 2.2.编译安装 下载 解压 wget https://tuxera.com/opensourc ...

  6. Duizi and Shunzi HDU

    Duizi and Shunzi Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. Codeforces Round #618 (Div. 1)A(观察规律)

    实际上函数值为x&(-y) 答案仅和第一个数字放谁有关 #define HAVE_STRUCT_TIMESPEC #include <bits/stdc++.h> using na ...

  8. nginx autoindex 配置目录浏览功能

    Nginx打开目录浏览功能 yum install httpd-tools -y cd /usr/local/openrestry/nginx/conf/ htpasswd -c passwd adm ...

  9. C#常用集合

    数组的缺点:长度固定.因此引入集合的使用. 注:泛型集合更安全,性能更高. 常用集合 对应泛型 ①动态数组ArrayList    List<T> 常用方法属性:Add  Clear  C ...

  10. jvm字节码助记符

    反编译指令 javap -c xxxx.class JVM参数设置 -xx:+<option>                  开启option -xx: -<option> ...