gpt4 book ai didi

java - 如何解析文本文件并从中创建数据库记录

转载 作者:行者123 更新时间:2023-12-01 14:33:36 25 4
gpt4 key购买 nike

我正在尝试基本上制作一个简单的测试生成器。我想要一个按钮来解析文本文件并将记录添加到我的数据库中。问题和答案位于文本文件中。我一直在网上搜索示例,但找不到符合我情况的示例。

文本文件中的标题信息我想忽略,直到以“~ End of Syllabus”开头的行为止。我想要“〜教学大纲结束”来指示问题的开始。之后的几行在第七个字符位置查找带有“(”的行。我希望它指示问题编号行。问题编号行的独特之处在于“(”位于第七个字符位置。我想用它作为指示符来标记新问题的开始。在问题编号行中,前三个字符“T1A”一起表示问题组。T1A*01* 是该组中的问题编号。

因此,正如您所看到的,我还需要获取实际的问题文本行和答案行。通常,在四个答案行之后是由“~~”指示的问题终止符。我不知道如何针对文本文件中的所有问题执行此操作。我是否继续将它们添加到数组字符串中?我如何从文件中访问此信息并将其添加到数据库中。这对我来说非常令人困惑,我觉得我可以通过查看涵盖我的情况的示例来了解其工作原理。这是我正在讨论的文本文件的链接:http://pastebin.com/3U3uwLHN

代码:

public static void main(String args[]) {

String endOfSyllabus = "~ End of Syllabus";
Path objPath = Paths.get("2014HamTechnician.txt");
String[] restOfTextFile = null;

if (Files.exists(objPath)){

File objFile = objPath.toFile();
try(BufferedReader in = new BufferedReader(
new FileReader(objFile))){

String line = in.readLine();
List<String> linesFile = new LinkedList<>();

while(line != null){
linesFile.add(line);
line = in.readLine();
}

System.out.println(linesFile);
}
catch(IOException e){
System.out.println(e);
}
}
else{
System.out.println(
objPath.toAbsolutePath() + " doesn't exist");
}

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new A19015_Form().setVisible(true);
}
});
}

最佳答案

在 Java 中读取文本文件非常简单(而且肯定还有其他更具创意/高效的方法来实现此目的):

try (BufferedReader reader = new BufferedReader(new FileReader(path))) { //try with resources needs JDK 7 

int lineNum = 0;
String readLine;
while ((readLine = reader.readLine()) != null) { //read until end of stream

跳过任意数量的行可以这样完成:

   if (lineNum == 0) {
lineNum++;
continue;
}

你真正的问题是要分割的文本。如果您一直在使用 CSV,则可以使用 String[] nextLine = readLine.split("\t"); 根据制表符分隔将每一行拆分为各自的单元格。但你不是,所以你将不得不阅读每一行,而不是找到一些可以拆分的内容。

看起来您可以控制文本文件格式。如果是,请使用更易于使用的格式,例如 CSV,否则您将需要为您的格式设计自定义解析器。

使用 CSV 的好处是它可以非常有效地镜像数据库。 IE。您的 CSV 标题列 = 数据库列。

就数据库而言,使用 JDBC 非常简单,只需确保使用准备好的语句插入数据以防止 SQL 注入(inject)即可:

     public Connection connectToDatabase(){
String url = "jdbc:postgresql://url";
return DriverManager.getConnection(url);
}

Connection conn = connectToDatabase();
PreparedStatement pstInsert = conn.prepareStatement(cInsert);
pstInsert.setTimestamp(1, fromTS1);
pstInsert.setString(2, nextLine[1]);
pstInsert.execute();
pstInsert.close();
conn.close();

--编辑--

我之前没有看到你的pastebin。您似乎并不负责文件格式,因此您将需要按空格(每个单词)进行分割,并依靠正则表达式来确定这是否是一个问题。幸运的是,该文件似乎相当一致,因此您应该能够在没有太多问题的情况下执行此操作。

--编辑2--

作为一种可能的解决方案,您可以尝试以下未经测试的代码:

try{
BufferedReader reader = new BufferedReader(new FileReader("file.txt")); //try with resources needs JDK 7

boolean doRegex = false;
String readLine;
while ((readLine = reader.readLine()) != null) { //read until end of stream
if(readLine.startsWith("~~ End of Syllabus")){
doRegex = true;
continue; //immediately goto the next iteration
}
if(doRegex){
String[] line = readLine.split(" "); //split on spaces
if(line[0].matches("your regex here")){
//answer should be line[1]
//do logic with your answer here
}
}
}
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}

关于java - 如何解析文本文件并从中创建数据库记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16678225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com