文件内容关键字检索与查找下一个(上一个)实现

private List returnList = new ArrayList();//在方法外定义一个存储的全局变量
public String logSearch(Long before,Long after,String findStr,String path) {
//before为关键字前多少行,after为关键字后多少行,findstr为查找的关键字,path为查找的文件路径

    //参数校验
    if(before == null){
        before = 0L;
    }
    if(after == null){
        after = 0L;
    }
    if(StringUtils.isEmpty(findStr)){
        throw new BusinessException("查找的字段为空");
    }
    if(path == null){
        throw new BusinessException("文件的路径为空");
    }

    returnList.clear();//全局变量清空

    String result = "";

    StringBuilder strLink = new StringBuilder();

    BufferedReader readerLink = null;
    try {
        readerLink = new BufferedReader(new InputStreamReader(new FileInputStream(path), "GB2312"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new BusinessException("文件转码异常");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        throw new BusinessException("未找到指定文件");

    }

    int len = 0; //读取字符
    Long i = 0L; //定位要查询字符前一位起始位置
    Long k = 1L; //定位行数(总行数与查询字符出现的行数)
    Long count = 0L;
    int h = 1; //存储集合的索引key

    Map linkMap = new HashMap(); //存放关键行数
    Map countMap = new HashMap();//存放每行要跳过的字符数
    countMap.put(1L,0L); //初始化
    while (true) {
        try {
            if (!((len = readerLink.read()) != -1)){ break;};
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("IO异常");
        }
        char lenChar = (char) len;
        strLink.append(lenChar);
        if (lenChar == 10) { //换行符
            if (strLink.toString().contains(findStr)) {

                linkMap.put(h,k);//h为key索引,解析为第x个,k为关键行数
                h++;

            }
            int charSize = strLink.length();
            count+=charSize;
            countMap.put(k+1,count);//第k+1行,需要跳过xx字符
            strLink = new StringBuilder();
            k++;
        }

        i++;
    }

    int size = linkMap.size();

    for (int n = 1; n < size+1; n++) {
        // 以跳过的字符数控制开头,以行数k控制结尾
        Long hang  = (Long) linkMap.get(1);

        if(hang == null){
            throw new BusinessException("未查询到结果");
        }

        Long total = 1L;
        Long chu = 0L;
        Long charSize = 0L;

        if(hang == 1L){ //指针与顶重合

            charSize = 0L;
            total = 1 + after;

        }else if(before >= hang || hang == before+1) {  //触顶或破顶,使用新的逻辑

            before = hang - 1;
            chu = hang - before;
            charSize = (Long) countMap.get(chu);
            total = before + 1 + after;

        }else {//无触顶,普通逻辑(包含before为0,after为0的特殊情况)

            chu = hang-before;
            charSize  = (Long) countMap.get(chu);
            total = before + 1 + after;
        }

        StringBuilder str = new StringBuilder();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "GB2312"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new BusinessException("文件转码异常");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new BusinessException("未找到指定文件");
        }
        len = 0;
        if(charSize != 0){
            try {
                reader.skip(charSize);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Long j = 1L;//内置计数器
        while (true) {
            try {
                if (!((len = reader.read()) != -1)){ break;};
            } catch (IOException e) {
                e.printStackTrace();
                throw new BusinessException("IO异常");
            }
            boolean flag = false;//外置控制器->内循环控制外循环
            char lenChar = (char) len;
            str.append(lenChar);
            if (lenChar == 10) {//换行符

                if(j.equals(total)){
                    flag =true;
                }
                j++;
            }

            if(flag){
                break;
            }

        }

        if(n == 1){//默认返回查询到的第一个结果

            result = str.toString();
        }

        returnList.add(str.toString());
    }


    return result;

}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值