获取的截图:
解析txt文件
数据库的存储
1.通过txt文件获取需要查看的文章标题和文字
2.正则表达式匹配对应的ABCD和标题选项
3.保存到 数据库
4.代码
package coma.ljx;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class zhuangHuang {
public static void main(String[] args) throws IOException {
aaa();
}
public static void aaa() throws IOException {
// CeShiMapper ceShiMapper = DaoXML.Dao(CeShiMapper.class);//dao数据库 这个可以不用可以自己写
//通过txt文件获取需要查看的文章标题和文字
String filePath="C:\\Users\\hbl\\Desktop\\JAVA\\项目全套\\主要项目\\SummaryMG\\src\\main\\resources\\ccc.txt";
FileInputStream fin = new FileInputStream(filePath);
InputStreamReader reader = new InputStreamReader(fin);
BufferedReader buffReader = new BufferedReader(reader);
String strTmp = "";
String strTmp22 = "";
while((strTmp = buffReader.readLine())!=null){ //读取文章
strTmp22+=strTmp+"\n";
}
//正则表达式匹配对应的ABCD和标题选项
String regex = "(\\d+\\..+?)\\s+([A-D]\\..+?)\\s+([A-D]\\..+?)\\s+([A-D]\\..+?)\\s+([A-D]\\..+?)\\s";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(strTmp22);
int i=1; //定义序号
while (matcher.find()) { //获取正则表达式正则出来的值 question代表标题 A,B,C,D代表
String question = matcher.group(1);
String optionA = matcher.group(2); //选项A
String optionB = matcher.group(3);//选项B
String optionC = matcher.group(4);//选项C
String optionD = matcher.group(5);//选项D
// Mybatis 保存到 数据库 可以自己修改成自己数据库的这句话不需要
// ceShiMapper.insertSelective(new CeShi(i,question,optionA,optionB,optionC,optionD,"毛概")); //修改的时候如果有值为null那么就不会修改对应这个数据的值
//打印
System.out.println("Question: " + question);
System.out.println("Option A: " + optionA);
System.out.println("Option B: " + optionB);
System.out.println("Option C: " + optionC);
System.out.println("Option D: " + optionD);
System.out.println();
i++;
}
buffReader.close(); //关闭流操作
}
}