一、关于Java流速度问题。

一、关于Java流速度问题。

1. 排名(快 -------> 慢):

  1. 缓冲流(BufferedReader/BufferedWriter)

  2. 混合使用转换流和缓冲流

  3. 转换流(InputStreamReader/OutputStreamWriter)

2. 测试代码如下:

  • 下面代码中字节输入流(FileInputStream)和字节输出流(FileOutpuStream)没做处理。
import java.io.*;

public class TestIOMain {
    public static void main(String[] args) {

        String oldFile = "C:\\Users\\Yu\\Desktop\\sunshine.txt";
        String newFile = "C:\\Users\\Yu\\Desktop\\";
        bufferedIOTest(oldFile,newFile+"buffreed.txt");
        changeIOTest(oldFile,newFile+"changeIO.txt");
        mixUseIO(oldFile,newFile+"mixUserIO.txt");
    }
    public static void mixUseIO(String oldFile,String newFile){
        // 一起使用
        long s = System.currentTimeMillis();
        // 转换流
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;

        // 缓冲流
        BufferedReader br = null;
        BufferedWriter bw = null;

        char[] chars = new char[1024];
        int len;
        try {
            // 输入流
            isr = new InputStreamReader(new FileInputStream(oldFile),"gbk");
            br = new BufferedReader(isr);

            // 输出流
            osw = new OutputStreamWriter(new FileOutputStream(newFile),"gbk");
            bw = new BufferedWriter(osw);
            while ((len = br.read(chars))!=-1){
                bw.write(chars,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {

            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (isr!=null){
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (osw!=null){
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("mixUseIO:"+(System.currentTimeMillis()-s));
    }
    public static void changeIOTest(String oldFile,String newFile){
        long s = System.currentTimeMillis();
        // 转换流
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        char[] chars = new char[1024];
        int len;
        try {
            isr = new InputStreamReader(new FileInputStream(oldFile), "gbk");
            osw = new OutputStreamWriter(new FileOutputStream(newFile),"gbk");
            while ((len = isr.read(chars))!=-1){
                osw.write(chars,0,len);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (isr!=null){
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (osw!=null){
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("changeIOTest:"+(System.currentTimeMillis()-s));
    }
    public static void bufferedIOTest(String oldFile,String newFile){
        long s = System.currentTimeMillis();
        // 缓冲流
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        byte[] bytes = new byte[1024];
        int len;
        try {
            bis = new BufferedInputStream(new FileInputStream(oldFile));
            bos = new BufferedOutputStream(new FileOutputStream(newFile));
            while ((len=bis.read(bytes)) != -1){
                bos.write(bytes,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("bufferedIOTest:"+(System.currentTimeMillis()-s));
    }

}

3. 测试结果(多次结果较为接近):

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值