NIO实战(一)——文本文件的按行读取

cbd2e2f0-e03c-41b0-ad9c-43f8d9bc0b8d|2016-08-03 15:04:52|95.159.37.41
1109df6c-864d-453c-9263-6b4380965edf|2016-08-03 15:04:52|78.179.103.21
1109df6c-864d-453c-9263-6b4380965edf|2016-08-03 15:04:52|176.94.192.2
eb234e9a-de9c-49e9-b616-3c84dee2fc34|2016-08-03 15:04:52|101.26.217.88
e83fc85d-8435-45a6-808a-92be00f3509a|2016-08-03 15:04:52|16.220.110.56
d45af9b3-9181-4c94-ab3c-d3f2f1af2120|2016-08-03 15:04:52|49.207.129.171
1109df6c-864d-453c-9263-6b4380965edf|2016-08-03 15:04:52|158.92.151.64
dc62d88b-13d2-4243-ace9-c5be14c2bf62|2016-08-03 15:04:52|220.184.142.190
28c0781a-469a-4142-8cf3-4deabf2eabe5|2016-08-03 15:04:52|132.216.255.83
df71c7ad-9b55-42e7-b47a-46c1d9980b0c|2016-08-03 15:04:52|41.70.28.115
6da67369-8a7b-4ddb-be32-f8ee1b1b158d|2016-08-03 15:04:52|243.181.213.214
28f090b0-3e4f-4cee-831a-2b4c6db7b938|2016-08-03 15:04:52|59.184.44.139
624ab136-06ec-47dc-8af5-d73378de2983|2016-08-03 15:04:52|110.248.145.138
5816cd62-62b1-4bb4-a584-4f2be38025e2|2016-08-03 15:04:52|175.196.173.202
308fa883-1d0b-4c80-b257-20a57798018b|2016-08-03 15:04:52|56.157.124.18
87c2f5a4-8119-4c99-8f61-b191156205c5|2016-08-03 15:04:52|86.152.141.124
cfd86ed2-8a09-4906-b61e-9cb1c83cfc2b|2016-08-03 15:04:52|132.185.44.243

今天想学一下NIO,网上搜到的都是一些概念呀什么的,看完了还是有种无从下手的感觉。需求是这样的,很简单,把上面的这些数据读出来,按行解析,直接贴代码。

    /**
     * NIO获得所有记录
     * @param file
     * @return 一个对象对应的hashmap的list
     * @throws Exception
     */
    public List getRecordListByNIO(File file) throws Exception {

        int bufSize = 1024 * 1024 * 5;
        FileChannel fcin = new RandomAccessFile(file, "r").getChannel();
        ByteBuffer rBuffer = ByteBuffer.allocate(bufSize);

        List<HashMap<String, String>> recordList = new ArrayList<HashMap<String, String>>();
        String enterStr = "\n";

        byte[] bs = new byte[bufSize];
        String tempString = null;
        //通过通道读到buffer中去
        while (fcin.read(rBuffer) != -1) {
            //position可以视为游标的作用,默认读后在尾部获得buffer大小
            int rSize = rBuffer.position();
            //将position设为0
            rBuffer.rewind();
            //将buffer中的数据转化成byte数组
            rBuffer.get(bs);
            rBuffer.clear();
            //转化成字符串
            tempString = new String(bs, 0, rSize);
            int fromIndex = 0;
            int endIndex = 0;
            //对数据进行分行,即endIndex指向换行符
            while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) {
                String recordStr = tempString.substring(fromIndex, endIndex);
                recordList.add(IOHandler.parseRecord(recordStr));<span style="white-space:pre">	</span>//<span style="font-family: Arial, Helvetica, sans-serif;">IOHandler.parseRecord(String)是一个处理这条字符串的方法</span>
//                System.out.println("line:" + recordStr);
                fromIndex = endIndex + 1;
            }
        }
        return recordList;
    }








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值