Java-IO流(2)(字节缓冲输入流、输出流;字符缓冲流)原始流高级流性能分析

个人简介

  • 大家好,我是翰慧腾。一名正在努力学JAVA的大一小白,本文章为初学的笔记,希望各位多多指教。💙
  • 欢迎点赞+收藏+留言💜
  • 请勇敢一点向前走,加油,亲爱的你~🧡

一、缓冲流

概述:缓冲流也称为高效流、高级流;之前学习的流称为原始流。

作用:缓冲流自带缓存区,可以提高原始字节流、字符流读写数据的性能。

二、字节缓冲输入流、输出流(BufferedInputStream类)

字节缓冲输入流自带了8KB缓冲池,数据就直接写入到缓冲池中去,我们可以直接从缓冲池读取数据,因此数据性能较好。提高了字节输入流读取数据的性能,读写功能上并无变化。

构造方法:

用缓冲流完成文件的拷贝提高其性能:

import java.io.*;

/**
 * @author hanhan
 * date 2022/4/13 19:52
 * 努力已经来不及了,你得拼命
 */
public class BufferedInputStream_Demo {
    public static void main(String[] args) throws Exception {
        InputStream f = new FileInputStream("C:\\one.png");
        //把原始的输入流包装成高级的缓冲字节输入流
        BufferedInputStream f1 = new BufferedInputStream(f);
        OutputStream ff = new FileOutputStream("D:\\two.png");
        //把原始的输出流包装成高级的缓冲字节输出流
        BufferedOutputStream ff1 = new BufferedOutputStream(ff);
        File file = new File("C:\\one.png");
        byte[] b=new byte[(int)file.length()];
        int len;
        while((len=f1.read(b))!=-1){
            ff1.write(b);
        }
        System.out.println("复制成功了");
        ff1.close();
    }
}

三、字节缓冲流的性能分析

copy一分JAVA视频资料。视频内存如图:

 代码性能分析:

import java.io.*;

/**
 * @author hanhan
 * date 2022/4/13 20:29
 * 努力已经来不及了,你得拼命
 */
public class ByteBufferedTime_Demo {
    private static final String SRC_FILE="D:\\JAVA语言\\JAVA桌面\\视频讲解(229集)\\1\\1.1 Java简介.mp4";
    private static final String DEST_FILE="D:\\HTML\\";
    public static void main(String[] args) {
        copy1();
        copy2();
        copy3();
    }
    /**
        使用低级的字符数组字节形式复制
     */
    public static void copy1(){
        long startTime=System.currentTimeMillis();
        try(
                //创建低级的字节输入流与源文件接通
                InputStream f = new FileInputStream(SRC_FILE);
                //创建低级的字节输出流与目标文件接通
                OutputStream ff = new FileOutputStream(DEST_FILE+"video1");
                ){
            //定义一个变量len记录每次读取的字节
            int len;
            byte[] b=new byte[1024];
            while((len=f.read(b))!=-1) {
                ff.write(b);
               // System.out.println(new String(b,0,len));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        long endTime=System.currentTimeMillis();
        System.out.println("使用低级的字节数组复制文件耗时:"+(endTime-startTime)/1000.0+"s");
    }
    /**
     使用高级的字符数组字节形式复制
     */
    public static void copy2(){
        long startTime=System.currentTimeMillis();
        try(
                //创建低级的字节输入流与源文件接通
                InputStream f = new FileInputStream(SRC_FILE);
                InputStream i=new BufferedInputStream(f);
                //创建低级的字节输出流与目标文件接通
                OutputStream ff = new FileOutputStream(DEST_FILE+"video2");
                OutputStream ii=new BufferedOutputStream(ff);
        ){
            //定义一个变量len记录每次读取的字节
            int len;
            byte[] b=new byte[1024];
            while((len=i.read(b))!=-1) {
                ii.write(b);
                // System.out.println(new String(b,0,len));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        long endTime=System.currentTimeMillis();
        System.out.println("使用高级的字节数组字复制文件耗时:"+(endTime-startTime)/1000.0);
}
    /**
     使用高级一个一个字节的形式复制
     */
    public static void copy3(){
        long startTime=System.currentTimeMillis();
        try(
                //创建低级的字节输入流与源文件接通
                InputStream f = new FileInputStream(SRC_FILE);
                InputStream i=new BufferedInputStream(f);
                //创建低级的字节输出流与目标文件接通
                OutputStream ff = new FileOutputStream(DEST_FILE+"video3");
                OutputStream ii=new BufferedOutputStream(ff);
        ){
            //定义一个变量len记录每次读取的字节
            int len;
            while((len=i.read())!=-1) {
                ii.write(len);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        long endTime=System.currentTimeMillis();
        System.out.println("使用高级的字节流一个一个字节的复制文件耗时:"+(endTime-startTime)/1000.0);
    }}

结果:

四、 字符缓冲输入流:提高字符输入流读取数据的性能,除此之外还提供了行读取的功能。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
 * @author hanhan
 * date 2022/4/14 16:20
 * 努力已经来不及了,你得拼命
 */
public class BufferedReader_Demo01 {
    public static void main(String[] args) {
        try(
                Reader r=new FileReader("src/dd.text");
                BufferedReader b = new BufferedReader(r);
        ) {
        String len;
        while((len=b.readLine())!=null){
            System.out.println(len);
        }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

 使用readLine方法读取行数据:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
 * @author hanhan
 * date 2022/4/14 10:28
 * 努力已经来不及了,你得拼命
 */
public class BufferedReader_Demo_ {
    public static void main(String[] args) {
        try(
                Reader f = new FileReader("src/dd.text");//该路径下的内容是《成名》歌词
                BufferedReader b = new BufferedReader(f);
        ) {
            int len;
            char[] c=new char[1024];
            while((len=b.read(c))!=-1){
                System.out.print(new String(c,0,len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

五、字符缓冲输出流 (BufferedWriter):提高字符输出流写取数据的性能,还提供了换行功能

六、案例:把《成名》的歌词顺序进行恢复到一个新文件。

原文件内容:

 

 

 代码:

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author hanhan
 * date 2022/4/14 19:05
 * 努力已经来不及了,你得拼命
 */
public class Copy_case {
    public static void main(String[] args) throws Exception {
        //创建字符缓冲输入流与源文件接通
        try(
                Reader f = new FileReader("src/Copy_case.text");
                BufferedReader b = new BufferedReader(f);
                BufferedWriter bb = new BufferedWriter(new FileWriter("D:/成名//geci.text"));
        ) {
            List<String> a = new ArrayList<>();
            String line;
            while ((line=b.readLine())!=null){
                a.add(line);
            }
            //如果是1.2.3.等等可以直接Collections.sort方法排序
            Collections.sort(a);
            //如果是一、二、三这样的排序,则不用采用上述方法,需要采用如下的方法,自定义一个比较规则
           /* List<String> aa = new ArrayList<>();
            Collections.addAll(aa,"一","二","三","四","五","六","七","八");
            Collections.sort(a, new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    return aa.indexOf(o1.substring(0,o1.indexOf(".")))-aa.indexOf(o2.substring(0,o2.indexOf(".")));
                }*/
            //输出集合
            for(String d:a){
                bb.write(d);
                bb.newLine();//换行
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

凌晨四点半sec

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

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

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

打赏作者

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

抵扣说明:

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

余额充值