【问题标题】:Comparing two text files in random order with Java用Java以随机顺序比较两个文本文件
【发布时间】:2017-01-05 14:34:15
【问题描述】:

我正在尝试比较两个随机的文本文件并打印出两个文件中匹配的行。 文件 1:

Student1
Student2
Student3
Student4

文件 2:

Student6
Student1
Student2

我希望输出为

Student1
Student2

我的代码如下。

public static void main(String[] args) throws IOException {

     String first = "file1.txt";
     String second = "file2.txt";
     BufferedReader fBr = new BufferedReader(new FileReader(first));
     BufferedReader sBr = new BufferedReader(new FileReader(second));   




     PrintWriter writer = new PrintWriter("test.txt", "UTF-8");  
     while ((first = fBr.readLine()) != null) {
         String partOne1 = fBr.readLine();
         String partTwo1 = sBr.readLine();
         while ((second = sBr.readLine()) != null) {
                System.out.println(first);
                writer.println(first);  
                break;                   

         }
     }


     writer.close();
     fBr.close();
     sBr.close(); 

【问题讨论】:

  • 您可以读取第一个文件中的所有行到ArrayList<String> 并在读取第二个文件时,如果第二个文件的每个字符串都存在于您的ArrayList 中,则通过方法tutorialspoint.com/java/util/arraylist_indexof.htm
  • 排序和合并...谷歌搜索

标签: java bufferedreader printwriter


【解决方案1】:

如果你想要一个优雅的解决方案:

  1. 对两者进行排序
  2. 作为排序列表进行比较

首先,这很简单。其次,排序优化得非常好,这通常比任何手动编写的代码都要快,并且生成优雅且易于理解的代码。

这里的大多数其他解决方案都是 O(n*m)。这种方法是 O(n log n + m log m),常数很小。您可以使用 hashmap 进行查找,理论上会产生 O(n + m) 但可能有太大的常数。

【讨论】:

    【解决方案2】:

    另一种选择是将两个文件放在两个数组列表中,并使用数组列表的 retainAll() 方法来获取公共文件。并对其进行打印或其他操作。

    public static void main(String[] args) throws IOException {
         String first = "file1.txt";
         String second = "file2.txt";
         BufferedReader fBr = new BufferedReader(new FileReader(first));
         BufferedReader sBr = new BufferedReader(new FileReader(second));   
    
         List<String> firstFile = new ArrayList<>();
         List<String> secondFile = new ArrayList<>();
    
         PrintWriter writer = new PrintWriter("test.txt", "UTF-8");  
         while ((first = fBr.readLine()) != null) {
             firstFile.add(first);
         }
         while ((second = sBr.readLine()) != null) {
             secondFile.add(second);                  
         }
    
         List<String> commonFile = new ArrayList<>(firstFile);
         commonFile.retainAll(secondFile);
         System.out.println(commonFile);
    
         writer.close();
         fBr.close();
         sBr.close(); 
    }
    

    【讨论】:

      【解决方案3】:

      如果您使用的是 Java8 ,以下是实现此逻辑的简洁方法。请注意,这仅适用于 Java8。它使用了一些 lambda 表达式和可用的功能,而无需大量样板代码。希望你觉得它至少很有趣

      List<String> file1Lines = Files.readAllLines(Paths.get("C:\\DevelopmentTools\\student-file1.txt"), Charset.defaultCharset());
      List<String> file2Lines = Files.readAllLines(Paths.get("C:\\DevelopmentTools\\student-file2.txt"), Charset.defaultCharset());
      
      List<String> matchingStrings = file1Lines.stream().
      filter(studentInfo -> file2Lines.contains(studentInfo))
                          .collect(Collectors.toList());
      matchingStrings.forEach(System.out::println);
      

      打印:

      Student1 , Student2
      

      【讨论】:

        【解决方案4】:

        这很简单=) 尝试存储第一个文件中的所有结果并与第二个文件中的所有行进行比较。会是这样的:

        package com.company;
        
        import java.io.BufferedReader;
        import java.io.FileReader;
        import java.io.IOException;
        import java.util.ArrayList;
        
        public class Main {
        
            public static void main(String[] args) throws IOException {
        
                String first = "file1.txt";
                String second = "file2.txt";
                BufferedReader fBr = new BufferedReader(new FileReader(first));
                BufferedReader sBr = new BufferedReader(new FileReader(second));
        
                ArrayList<String> strings = new ArrayList<String>();
        
                while ((first = fBr.readLine()) != null) {
                    strings.add(first);
                }
                fBr.close();
        
                while ((second = sBr.readLine()) != null) {
                    if (strings.contains(second)) {
                        System.out.println(second);
                    }
                }
                sBr.close();
            }
        }
        

        如果可能,最好使用内存,你在不同的 while 中的“while”可能会工作太长时间并混淆逻辑。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-29
          • 2020-10-11
          • 1970-01-01
          • 2023-04-07
          • 2011-08-25
          • 2010-09-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多