缓冲流【Buffered】

缓冲流【Buffered】

缓冲流我们理解为原来的使用数组方式进行数据传输的一种增强

按照类型分为:

  • 字符缓冲流:BufferedReader, BufferedWriter
  • 字节缓冲流:BufferedInputStream, BufferedOutputStream

缓冲流的基本原理,是在创建对象的时候,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写数据,减少系统IO操作的次数,减少开销,提高读写的效率。

字节缓冲流

构造方法

  • public BufferedInputStream(InputStream input) : 创建一个新的缓冲输入流
  • public BufferedInputStream(InputStream input,int size)
  • public BufferedOutputStream(OutputStream output) : 创建一个新的缓冲输出流
  • public BufferedOutputStream(OutputStream output,int size) : 指定大小

BufferedOutputStream使用步骤:

  1. 创建一个FileOutputStream流对象,构造方法中绑定要写入数据的目的地
  2. 创建BufferedOutputStream对象,构造方法中传入FileOutputStream流对象
  3. 使用BufferedOutputStream对象中的方法writer,把数据写入到内部缓冲区中
  4. 使用BufferedOutputStream对象中的方法flush,把内存缓冲区中的数据刷新到目的地中
  5. 释放资源

BufferedInputStream使用步骤:

  1. 创建一个FileInputStream流对象,构造方法中绑定需要写入数据的数据源
  2. 创建BufferedInputStream对象,构造方法中传递FileInputStream流对象
  3. 使用BufferedInputStream对象中的方法read,把数据读取到内部当中。
  4. 释放资源。|
import java.io.FileInputStream;
import java.io.IOException;

public class BufferedInput {
    public static void main(String[] args) throws IOException {
        FileInputStream fis=new FileInputStream("day29_IO\\one.txt");
        BufferedInputStream bis=new BufferedInputStream(fis);
        int len=0;
		/* while ((len= bis.read())!=-1){
            System.out.println((char)len);
        }*/
        byte[] bytes=new byte[1024];
        while ((len= bis.read(bytes))!=-1){
            System.out.println(new String(bytes,0,len));
        }
    }
}

字符缓冲流

构造方法

  • public BufferedWriter(Writer out) : 创建一个新的字符缓冲输出流
  • public BufferedReader(Reader oin) : 创建一个新的字符缓冲输入流

特有方法

  • BufferedReader: public String readLine() : 读取整行的文本信息
  • BufferedWriter : public void newLine() : 写入一行的行分隔符,由系统属性定义换行符号

使用步骤:

  1. 创建一个字符缓冲输出流对象,构造方法中传递一个字符输出流
  2. 调用字符缓冲输出流对象中的writer,把数据写入到内存缓冲区中
  3. 调用flush方法,把缓冲区中的数据刷新到文件中
  4. 释放资源
/*
    String readLine(): 读取一个文本行,读取一整行的数据
    返回值:
        包含该行内容的字符串,不包含任何行的终止符号,如果已经读到文件末尾,返回null
     使用步骤:
        1.创建一个字符缓冲输入流对象,构造方法中传递一个字符输入流
        2.使用字符缓冲输入流对象中的read/readLine,读取文本信息
        3.关闭
 */
public class DemoBufferedReader {
    public static void main(String[] args)throws IOException {
        BufferedReader reader=new BufferedReader(new FileReader("day29_IO\\abc.txt"));
        /*String s = reader.readLine();
        System.out.println(s);*/
        String str=null;
        while ((str= reader.readLine())!=null){
            System.out.println(str);
        }
        reader.close();
    }
}

字节流与缓冲字节流的比较

public class Test {
    public static void main(String[] args) throws IOException {
        show1();
        show2();
    }
    public static void show1()throws IOException{
        long start=System.currentTimeMillis();
        FileInputStream fis=new FileInputStream("C:\\Users\\LiuJunLiang\\Desktop\\3.gif");
        FileOutputStream fos=new FileOutputStream("C:\\Users\\LiuJunLiang\\Desktop\\新建文件夹\\1.gif");
        byte[] bytes=new byte[1024];
        int len=0;
        while ((len= fis.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }
        fos.close();
        fis.close();
        long end=System.currentTimeMillis();
        System.out.println(end-start);
    }
    public static void show2()throws IOException{
        long start=System.currentTimeMillis();
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("C:\\Users\\LiuJunLiang\\Desktop\\3.gif"));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("C:\\Users\\LiuJunLiang\\Desktop\\新建文件夹\\2.gif"));
        byte[] bytes=new byte[1024];
        int len=0;
        while((len= bis.read(bytes))!=-1){
            bos.write(bytes,0,len);
        }
        bos.close();
        bis.close();
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值