Java进阶之旅第十二天

Java进阶之旅第十二天

IO流

字节缓冲流

  • 字节缓冲流,在原有的字节流中添加一个缓冲区,用于读取/写入提升效率
构造方法
构造方法说明
public BufferedInputStream(InputStream is)字节缓冲输入流,参数是字节输入流的类
public BufferedInputStream(InputStream is,int size)字节缓冲输入流,参数是字节输入流的类,size指定缓冲区大小
public BufferedOutputStream(OutputStream os)字节缓冲输出流,参数是字节输出流的类
public BufferedOutputStream(OutputStream os,int size)字节缓冲输出流,参数是字节输出流的类,size指定缓冲区大小
  • 底层缓冲区的默认大小是8192字节大小

  • 代码展示

public static void main(String[] args) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("src\\a.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src\\b.txt"));
        //进行文件拷贝操作
        int b;
        while((b=bis.read())!=-1){
            bos.write(b);
        }
        bos.close();
        bis.close();
    }
  • 注意:
    • 字节缓冲流中的读或写方法和参数中写入的对象的读或写方法是一致的
    • 字符缓冲流提升效率的原理是,在内存中创建缓冲区,将读取的数据放入缓冲区中,从而减少从文件读取内容的时间,从而提升效率

字符缓冲流

构造方法
构造方法说明
public BufferedReader(Reader r)字符缓冲输入流,参数是字符输入流的类
public BufferedWriter(Writer r)字符缓冲输出流,参数是字符输出流的类
特有的方法
特有方法说明
public String readLine()读取一行数据,如果没有数据可读,就返回null
public void newLine()跨平台的换行
  • 底层缓冲区的默认大小是8192字节大小

练习

拷贝文件,比较用时(字节流)
import java.io.*;
import java.sql.Date;
import java.sql.Time;

// 按两次 Shift 打开“随处搜索”对话框并输入 `show whitespaces`,
// 然后按 Enter 键。现在,您可以在代码中看到空格字符。
public class Main {
    public static void main(String[] args) throws IOException {
        copy1("src\\all.png","src\\all1.png");
        copy2("src\\all.png","src\\all2.png");
        copy3("src\\all.png","src\\all3.png");
        copy4("src\\all.png","src\\all4.png");
    }
    //一次读写一个字节
    public static void copy1(String srcPath, String tarPath) throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream(srcPath);
        FileOutputStream fos = new FileOutputStream(tarPath);
        int len;
        while((len = fis.read())!=-1){
            fos.write(len);
        }
        fos.close();
        fis.close();
        long end = System.currentTimeMillis();
        System.out.printf("一次读写一个字节耗时%d\n",end-start);
    }
    //一次读写一个字节数组
    public static void copy2(String srcPath, String tarPath) throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream(srcPath);
        FileOutputStream fos = new FileOutputStream(tarPath);
        int len;
        byte[] data = new byte[128];
        while((len = fis.read(data))!=-1){
            fos.write(data,0,len);
        }
        fos.close();
        fis.close();
        long end = System.currentTimeMillis();
        System.out.printf("一次读写一个字节数组耗时%d\n",end-start);
    }
    //缓冲流: 一次读写一个字节
    public static void copy3(String srcPath, String tarPath) throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream(srcPath);
        FileOutputStream fos = new FileOutputStream(tarPath);
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int len;
        while((len = bis.read())!=-1){
            bos.write(len);
        }
        bos.close();
        bis.close();
        long end = System.currentTimeMillis();
        System.out.printf("缓冲流: 一次读写一个字节耗时%d\n",end-start);
    }
    //缓冲流: 一次读写一个字节数组
    public static void copy4(String srcPath, String tarPath) throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream(srcPath);
        FileOutputStream fos = new FileOutputStream(tarPath);
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int len;
        byte[] data = new byte[128];
        while((len = bis.read(data))!=-1){
            bos.write(data,0,len);
        }
        bos.close();
        bis.close();
        long end = System.currentTimeMillis();
        System.out.printf("缓冲流: 一次读写一个字节数组耗时%d\n",end-start);
    }
}
  • 结果
一次读写一个字节耗时2223
一次读写一个字节数组耗时17
缓冲流: 一次读写一个字节耗时13
缓冲流: 一次读写一个字节数组耗时2
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不吃牛肉!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值