栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

一、关于Java流速度问题。

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一、关于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. 测试结果(多次结果较为接近):

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/922262.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号