文档内容处理,筛选读取并写入指定文件内

8 篇文章 0 订阅
/**
     * 根据子文件内容来筛选父文件内容
     * 将筛出来的数据写入指定的txt文件中
     *
     * @param fileA 父文件
     * @param fileB 子文件
     * @param pathC 上传的数据筛选路径
     * @param pathD 增量的数据筛选路径
     */
    public static void screenData(String fileA, String fileB, String pathC, String pathD) {
        try {
            //开始时间
            long startTime = System.currentTimeMillis();
            BufferedWriter out = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(pathC, true)));
            BufferedWriter outD = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(pathD, true)));
            File fA = new File(fileA); //父文件
            File fB = new File(fileB); //子文件
            File fC = new File(pathC); //目标文件
            //文件流
            FileInputStream fisA = new FileInputStream(fA);
            FileInputStream fisB = new FileInputStream(fB);
            //输入流
            InputStreamReader isrA = new InputStreamReader(fisA, "utf-8");
            InputStreamReader isrB = new InputStreamReader(fisB, "utf-8");
            //缓存
            BufferedReader brA = new BufferedReader(isrA);
            BufferedReader brB = new BufferedReader(isrB);
            List<String> tA = new ArrayList<>(); //父文件
            List<String> tB = new ArrayList<>(); //子文件
            List<String> tC = new ArrayList<>(); //已上传的文件
//            List<String> tD = new ArrayList<>(); //未上传的文件
            String line = "";
            //如果没有目标文件,则创建目标文件
            if (!fC.exists()) {
                fC.createNewFile();
            }
            while ((line = brB.readLine()) != null) {
                tB.add(line);
            }
            while ((line = brA.readLine()) != null) {
                tA.add(line);
            }
            //将已上传的数据加入文档中
            for (String a : tA) {
                for (String b : tB) {
                    if (a.contains(b)) {
                        tC.add(a);
//                        out.write(a + '\n');
                    }
                }
            }
            List<String> lists = removalDuplicate(tC);
            for (String list : lists) {
                out.write(list + '\n');
            }
            tA.removeAll(tC);
            for (String a : tA) {
                outD.write(a+'\n');
            }
            out.close();
            outD.close();
            long endTime = System.currentTimeMillis();
            System.out.println("程序执行时间:" + (endTime - startTime) + "ms");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
/**
     * list集合元素去重
     * @param data 要去重的list集合
     */
    public static List<String> removalDuplicate(List<String> data) {
        String str = "";
        List<String> list = new ArrayList<>();
        for (String datum : data) {
            Matcher matcher = Pattern.compile("(\\\\)(\\d)+.jpg").matcher(datum);
            if (matcher.find()) {
                str = datum.replace(matcher.group(), "");
                list.add(str);
            }
//            System.out.println("截取后的字符串:" + str);
        }
        LinkedHashSet<String> hashSet = new LinkedHashSet<>(list);
        ArrayList<String> listWithoutDuplicates = new ArrayList<>(hashSet);
//        System.out.println("去重后的数据:"+listWithoutDuplicates);
        System.out.println("去重前的数量:" + data.size());
        System.out.println("去重后的数量:"+listWithoutDuplicates.size());
        return listWithoutDuplicates;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人在西北刚上线

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值