0

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

文本文件包含我想忽略的标题信息,直到以“〜教学大纲结束”开头的行。我想用“~ 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);
        }
    });
}
4

1 回答 1

1

用 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 的一个好处是它可以非常有效地镜像数据库。即您的 CSV 标题列 = 数据库列。

就数据库而言,使用 JDBC 很容易,只需确保使用准备好的语句插入数据以防止 SQL 注入:

     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.
    }
于 2013-05-21T20:07:53.313 回答