IO读取速度测试

不知道是什么情况,今天喜欢上了看IO这个东东。。回来码了几行简单的不能再简单的代码 ,跑了一把,得了几个 数据 ,放到这里跟大家分享下。。

先把测试的结果截图摆上来欣赏一下:有一点需要说明的是,前四个方法是以字节流的形式读取一个大小为11M左右的rar文件,后面两个 方法是以字符流的形式读取在小在1M~2M之间的一个文本文件~



具体每个方法是怎么实现 的,代码简单到了什么程度,常用到了什么程度 。。。。。。

话不多说,元芳,代码在这里,你怎么看?

public class IOSpeedTest {
    private static final String file = "G:\\pt.rar";

    @Test
    public void testWithoutBuf() throws IOException {
        int line = 0;
        FileInputStream in = new FileInputStream(file);
        int bit = -1;
        while ((bit = in.read()) != -1) {
            if (bit == '\n') {
                ++line;
            }
        }
        if (in != null) {
            in.close();
        }
        System.out.println(line);
    }

    @Test
    public void testWithBufferedIn() throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        int line = 0;
        int bit = -1;
        while ((bit = in.read()) != -1) {
            if (bit == '\n') {
                ++line;
            }
        }
        if (in != null) {
            in.close();
        }
        System.out.println(line);
    }

    @Test
    public void testWithFixedBuf() throws IOException {
        int bufSize = 8196;
        FileInputStream in = new FileInputStream(file);
        int line = 0;
        byte[] buf = new byte[bufSize];
        while (in.read(buf) != -1) {
            for (byte element : buf) {
                if (element == '\n') {
                    ++line;
                }
            }
        }
        if (in != null) {
            in.close();
        }
        System.out.println(line);
    }

    @Test
    public void testWithWholeFile() throws IOException {
        FileInputStream in = new FileInputStream(file);
        int line = 0;
        byte[] buf = new byte[file.length()];
        in.read(buf);
        for (byte element : buf) {
            if (element == '\n') {
                ++line;
            }
        }
        if (in != null) {
            in.close();
        }
        System.out.println(line);
    }

    private static final String txt = "G:\\cy.txt";

    @Test
    public void testWithBf() throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(txt)));
        while (bf.readLine() != null) {

        }
    }

    @Test
    public void testWithFixedBf() throws IOException {
        int bufSize = 8196;
        InputStreamReader reader = new InputStreamReader(new FileInputStream(txt));
        char[] buf = new char[bufSize];
        while (reader.read(buf) != -1) {

        }
        reader.close();
    }

}

好长时间没有自己写过文章了,今天写这么水的东西出来,求轻喷。。~~

kevin 11/29/2012

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值