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 "";
}
}