用代码快速帮家人实现word题目与答案匹配的可用方案

package com.company;

import com.sun.xml.internal.ws.util.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        Map<String,String> answer=new HashMap<>();
        System.out.println("123");
        //缓存答案
        String answerFile = "C:\\/myword/answer.txt";

        File sourceanswerFile = new File(answerFile);

        // 检查文件是否存在
        if (!sourceanswerFile.exists()) {
            System.out.println("-- Sorry,source file:" + sourceanswerFile
                    + " can't be found. --");
            System.exit(0);
        }

        Scanner input = new Scanner(sourceanswerFile);
        while (input.hasNext()) {
            String newStr = input.nextLine();
            String[] array=newStr.split(";");
            if(array.length>1){
                answer.put(array[0],newStr);
            }else{
                System.out.println(newStr);
            }
        }
        input.close();
        System.out.println("--- finish ----"+answer.get("123"));
        //String tempint=getNumbers("1234ererrfd888");
        System.out.println("--- finish answer----");
        String sourceFile = "C:\\/myword/tq1.txt";
        String targetFile = "C:\\/myword/tq2.txt";
        File source = new File(sourceFile);
        File target = new File(targetFile);
        // 检查文件是否存在
        if (!source.exists()) {
            System.out.println("-- Sorry,source file:" + sourceFile
                    + " can't be found. --");
            System.exit(0);
        }

        Scanner inputsourceFile = new Scanner(source);
        PrintWriter writer = new PrintWriter(target);
        while (inputsourceFile.hasNext()) {
            String newStr=inputsourceFile.nextLine();
            if(null!=newStr&&newStr.length()>0){
                String tempint=getNumbers(newStr);
                if(null!=tempint&&tempint.length()>=0){
                    String anStr=answer.get(tempint);
                    if(null!=anStr&&anStr.length()>0){
                        newStr= newStr.replaceAll(tempint, anStr);
                    }
                }
            }
            writer.write(newStr + "\n");
        }
        inputsourceFile.close();
        writer.close();
        System.out.println("--- finish ----");
        System.out.println("--- finish ----");
    }

    //截取数字  【读取字符串中第一个连续的字符串,不包含后面不连续的数字】
    public static String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值