android中txt大文件读取指定部分数据

java的文件流读取,相信大家都会了,就不解释了,这里只是包装了一下,实现读取指定位置的数据。

1,读取指定几行的数据

    /**
     * 读取特定行的数据
     *
     * @param fileName    文件名
     * @param indexLines  开始行
     * @param lengthLines 要读取的行数
     * @return
     */
    public static String getStringByLines(String fileName, int indexLines, int lengthLines) {

        StringBuilder sb = new StringBuilder("");
        InputStreamReader inputStreamReader = null;
        try {
            if (fileName != null && fileName.startsWith(AppConfig.ASSET_LOAD_PATH)) {
                //处理asset中的文件  MyApp.getInstance()得到context
                //注意ASSET_LOAD_PATH  为我自定义的asset路径名 file:///android_asset/ 和webview的导入asset路径相同
                inputStreamReader = new InputStreamReader(MyApp.getInstance().getAssets().open(fileName.substring(AppConfig.ASSET_LOAD_PATH.length())));
            } else {
                inputStreamReader = new FileReader(fileName);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        if (inputStreamReader == null) return sb.toString();

        BufferedReader reader = new BufferedReader(inputStreamReader);
        String line;
        long currLines = 0;
        try {
            while ((line = reader.readLine()) != null) {
                if (indexLines > currLines) {
                    //如果读取的起始位置 还未达到
                    currLines++;
                    continue;
                }
                if (currLines >= indexLines + lengthLines) {
                    sb.append(line);
                    sb.append("\n");
                    break;
                } else {
                    sb.append(line);
                    sb.append("\n");
                }
                currLines++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) reader.close();
            } catch (IOException e) {
            }
        }
        return sb.toString();
    }

 

2,读取指定位置的数据

    /**
     * 读取txt文件内容
     */
    public static String getString(String fileName) {
        return getString(fileName, 0, Long.MAX_VALUE);
    }

    /**
     * 读取txt文件 内容
     *
     * @param fileName 文件名
     * @param index    txt的起始位置
     * @param length   要读取字段的长度
     * @return
     */
    public static String getString(String fileName, long index, long length) {

        StringBuilder sb = new StringBuilder("");
        InputStreamReader inputStreamReader = null;
        try {
            if (fileName != null && fileName.startsWith(AppConfig.ASSET_LOAD_PATH)) {
                //处理asset中的文件  MyApp.getInstance()得到context
                //注意ASSET_LOAD_PATH  为我自定义的asset路径名 file:///android_asset/ 和webview的导入asset路径相同
                inputStreamReader = new InputStreamReader(MyApp.getInstance().getAssets().open(fileName.substring(AppConfig.ASSET_LOAD_PATH.length())));
            } else {
                inputStreamReader = new FileReader(fileName);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        if (inputStreamReader == null) return sb.toString();

        BufferedReader reader = new BufferedReader(inputStreamReader);
        String line;
        long currLen = 0;
        try {
            while ((line = reader.readLine()) != null) {
                int lineLen = line.length();
                if (index > currLen + lineLen) {
                    //如果读取的起始位置 还未达到
                    currLen += line.length() + 1;//多了 换行符
                    continue;
                }
                //逻辑小复杂  自己拿笔画一下
                if (currLen + lineLen >= index + length) {
                    //结束
                    int len = (int) (currLen + lineLen - index - length);
                    sb.append(line.substring(0, lineLen - len));
                    sb.append("\n");
                    break;
                } else if (index > currLen && currLen + lineLen > index) {
                    //开始
                    sb.append(line.substring((int) (index - currLen), lineLen));
                    sb.append("\n");
                } else {
                    //中途 currLen + lineLen < index + length
                    sb.append(line);
                    sb.append("\n");
                }
                currLen += line.length() + 1;//多了 换行符
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) reader.close();
            } catch (IOException e) {
            }
        }
        return sb.toString();
    }

好了,搞定!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值