read bigfile

来源:互联网 发布:br软件怎么用 编辑:程序博客网 时间:2024/05/16 04:38

public List<String[]> readBigFile(String filePath)
    {
        List<String[]> object = new ArrayList<String[]>();
        int bufSize = 1024;
        byte[] bs = new byte[bufSize];
        ByteBuffer byteBuf = ByteBuffer.allocate(1024);

        FileChannel channel;
        try
        {
            channel = new RandomAccessFile(filePath, "r").getChannel();

            while (channel.read(byteBuf) != -1)
            {
                int size = byteBuf.position();
                byteBuf.rewind();
                byteBuf.get(bs);

                // 把文件当字符串处理
                String str = new String(bs, 0, size);

                String[] strList = str.split(",");
                // 可以写一个解析方法decode() or parser(),把数组的值对应放到对象里。

                byteBuf.clear();
                object.add(strList);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return object;

    }

原创粉丝点击