字节流、字符流读取速度

测试字节流、字符流读取速度

准备工作

写一个字符流读取文件,再写一个字节流读取文件

/**
 * @author dxy
 * @date 2020/1/9 13:05
 */
public class DemoIo {
	/**读取的文件地址,这是一个125 MB (131,143,320 字节)文件*/
    private static final String SRC = "C:\\Users\\Administrator\\Desktop\\RAF.txt";

    public static void main(String[] args) throws IOException {

        System.out.println("开始测试!");
        // BufferedReader字符流读取文件
        bufferedReader();

        // 字节流读取文件
        inputStream();

        // 字节流读取文件(加入缓冲)
        BufferedInputStream();
    }

    /**
     * 为字符流专门打造的(适合字符的操作)
     *
     * @throws IOException
     */
    private static void bufferedReader() throws IOException {
        long start = System.currentTimeMillis();
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(new FileInputStream(SRC), StandardCharsets.UTF_8));
        try {
            while (true) {
                String s = bufferedReader.readLine();
                if (s != null) {
//                    System.out.println(s);
                } else {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        bufferedReader.close();
        long end = System.currentTimeMillis();
        System.out.println("bufferedReaderDemo耗时" + (end - start));
    }

    /**
     * 字节流读取文件(通用型的)
     *
     * @throws IOException
     */
    private static void inputStream() throws IOException {
        long start = System.currentTimeMillis();
        InputStream inputStream = new FileInputStream(SRC);

        byte[] bytes = new byte[1024];
        while (true) {
            int read = inputStream.read(bytes);
            if (read != -1) {
//                System.out.println(new String(bytes, 0, read));
            } else {
                break;
            }
        }
        inputStream.close();
        long end = System.currentTimeMillis();
        System.out.println("inputStream耗时" + (end - start));

    }

    /**
     * 字节流读取文件(通用型的)+加上缓冲
     *
     * @throws IOException
     */
    private static void BufferedInputStream() throws IOException {
        long start = System.currentTimeMillis();
        InputStream inputStream = new FileInputStream(SRC);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        byte[] bytes = new byte[1024];
        while (true) {
            int read = bufferedInputStream.read(bytes);
            if (read != -1) {
//                System.out.println(new String(bytes, 0, read));
            } else {
                break;
            }
        }
        inputStream.close();
        long end = System.currentTimeMillis();
        System.out.println("BufferedInputStream耗时" + (end - start));
    }


}

运行查看耗时时间

开始测试!
bufferedReaderDemo耗时994
inputStream耗时279
BufferedInputStream耗时70

Process finished with exit code 0
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值