块式读取与流式读取对比

块式读取与流式读取对比


代码示例
https://download.youkuaiyun.com/download/timliangl/10972192

块式读取

	private static final int BUFFER_SIZE = 1024*1024;
    public static List<String> channelReadBuffer(FileInputStream fin) throws IOException {
        List<String> list = new ArrayList<>();
        try(FileChannel source = fin.getChannel()) {
            ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
            int bytesRead;
            int countReadNum = 0;
            StringBuffer stringBuffer = new StringBuffer();
            while ((bytesRead = source.read(buffer)) != -1) {
                buffer.flip();
                while (buffer.hasRemaining()) {
                    char c = (char) buffer.get();

                    if (c == '\r') {
                        //过滤\n
                        buffer.get();
                        if (stringBuffer.length() == 0) {
                            list.add("");
                        } else {
                            list.add(stringBuffer.toString());
                        }
                        stringBuffer.delete(0, stringBuffer.length() - 1);
                    }
                    if (c == '\n') {
                        if (stringBuffer.length() == 0) {
                            list.add("");
                        } else {
                            list.add(stringBuffer.toString());
                        }
                        stringBuffer.delete(0, stringBuffer.length() - 1);
                    } else {
                        stringBuffer.append(c);
                    }
                }
                buffer.clear();

                countReadNum++;
            }
            log.info("channelReadBuffer read count num: " + countReadNum);
        }catch (Exception e){
            log.info("Exception");
        }
        return list;
    }

流式读取

    public static String readLine(BufferedReader br5)
        throws IOException
    {
        String lines = null;
        int intC;
        while ((intC = br5.read()) != -1)
        {
            char c = (char)intC;
            /**
             * 当字符为\r\n结束
             * 
             * add on 1.14,2014
             * 
             */
            if (c == '\r')
            {
                //过滤\n
                br5.read();
                if (lines == null)
                {
                    lines = "";
                }
                break;
            }
            if (c == '\n')
            {
                if (lines == null)
                {
                    lines = "";
                }
                break;
            }
            if (lines != null && lines.length() >= MAX_STR_LEN)
            {
                throw new IOException("readLine : input too long.");
            }
            if (lines == null)
            {
                lines = "";
            }
            lines += c;
        }
        return lines;
    }

代码示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值