java字符串匹配大文本文件问题

标签 java string file text

我想实现一个从大文本文件中进行字符串匹配的任务。 1.替换所有非字母数字字符 2. 统计文本文件中特定术语的数量。例如,匹配术语“tom”。匹配不区分大小写。所以术语“Tom”应该算在内。但是明天这个词不应该计算在内。

code template one:
    try {
           in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile));
        } catch (FileNotFoundException e1) {
           System.out.println("Not found the text file: "+inputFile);
         }
    Scanner scanner = null;
    try {
        while (( line = in.readLine())!=null){  
               String newline=line.replaceAll("[^a-zA-Z0-9\\s]", " ").toLowerCase();
               scanner = new Scanner(newline);
               while (scanner.hasNext()){
                       String term = scanner.next();
                   if (term.equalsIgnoreCase(args[1]))
                   countstr++;
               }
         }
     } catch (IOException e) {
    e.printStackTrace();
    }

code template two:
   try {
        in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile));
       } catch (FileNotFoundException e1) {
           System.out.println("Not found the text file: "+inputFile);
         }
   Scanner scanner = null;
   try {
        while (( line = in.readLine())!=null){  
               String newline=line.replaceAll("[^a-zA-Z0-9\\s]", " ").toLowerCase();
               String[] strArray=newline.split(" ");//split by blank space
                       for (int =0;i<strArray.length;i++)
                               if (strArray[i].equalsIgnoreCase(args[1]))
                                      countstr++;
               }
         }
     } catch (IOException e) {
    e.printStackTrace();
   }

通过运行这两个代码,我得到了不同的结果,扫描仪看起来得到了正确的结果。但是对于大型文本文件,扫描仪的运行速度比后者慢得多。谁能告诉我原因并给出更有效的解决方案。

最佳答案

在你的第一个方法中。您不需要使用两台扫描仪。对于大行来说,带有“”的扫描仪不是一个好的选择。

您的行已转换为小写。所以你只需要在外面做一次小写的 key 即可。并在循环中执行 equals

或者获取线路

String key = String.valueOf(".*?\\b" + "Tom".toLowerCase() + "\\b.*?");
        Pattern p = Pattern.compile(key);
        word = word.toLowerCase().replaceAll("[^a-zA-Z0-9\\s]", "");
        Matcher m = p.matcher(word);
        if (m.find()) {
            countstr++;
        } 

我个人会选择 BufferedReader 方法来处理大文件。

String key = String.valueOf(".*?\\b" + args[0].toLowerCase() + "\\b.*?");
         Pattern p = Pattern.compile(key);
         try (final BufferedReader br = Files.newBufferedReader(inputFile,
                    StandardCharsets.UTF_8)) {
                for (String line; (line = br.readLine()) != null;) {
                    // processing the line.
                    line = line.toLowerCase().replaceAll("[^a-zA-Z0-9\\s]", "");
                    Matcher m = p.matcher(line);
                    if (m.find()) {
                        countstr++;
                    }           
                }
         }

提供了 Java 7 中的示例。如果需要,请进行更改!

关于java字符串匹配大文本文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22925919/

相关文章:

java - Java vs Scala类型层次结构

java - 了解函数运算符 : Lambda

python - 拉取包含字符串的列的部分并创建字典,可以根据公共(public)键拉取值

java - 安卓:撤销权限

c - 如何在c中动态分配结构数组?

java - Play 框架 2.6 - Java : Ws request POST with oAuth Instagram

java - 准备好的 SQL 语句无效

java - 如何在迭代时从 HashMap 中删除一个键?

Ruby:按字母顺序对包含一些字符串数组的字符串数组进行排序

PHP文件上传无法在同一表单上发送POST数据