【问题标题】:Compare Strings from between 2 files using Java使用 Java 比较两个文件之间的字符串
【发布时间】:2015-08-17 05:42:15
【问题描述】:
public class comparee {

static int count=0;
public static void main(String[] args) throws IOException {

    String a,b;

    FileReader fi = new FileReader(new File("C:\\Users\\IBM_ADMIN\\Desktop\\SAM_PUBLIC_MONTHLY_20150802\\a.txt"));  // new file

    FileReader fii = new FileReader(new File("C:\\Users\\IBM_ADMIN\\Desktop\\SAM_PUBLIC_MONTHLY_20150802\\b.txt"));  // new file

    BufferedReader br =new BufferedReader(fi); //new

    BufferedReader br1 =new BufferedReader(fii); //old
    FileWriter fw = new FileWriter(new File("C:\\Users\\IBM_ADMIN\\Desktop\\SAM_PUBLIC_MONTHLY_20150802\\samnew.txt"));

    int count = 0;
    while((a=br.readLine()) != null)
    {

        while((b=br1.readLine()) != null)
        {

            if(!(a.equals(b))) 
            {
                count++;
                fw.write(a);
            }

        }
        System.out.println(count);

    }


}
}

您好,我正在尝试通过逐行读取来比较 a.txt 和 b.txt 中的字符串。 我想在 samdata.txt 上写一行,这在 a.txt 中不可用,但在 b.txt 中可用。将不胜感激任何帮助:) 谢谢

P.S以上代码逻辑不正确

【问题讨论】:

  • 这个练习的最终目标是什么?你能简单地使用无处不在的免费命令行diff 实用程序吗?
  • 将这两个文件读入Lists 和简单的removeAll 一个可能更容易...
  • 您的文件是否已排序,以便匹配的行必须位于同一位置?或者,对于 a.txt 的每一行,b.txt 中的相应匹配项可能位于任何地方?在每种情况下,解决方案都可能完全不同

标签: java string-comparison


【解决方案1】:

比较文件是一项复杂的操作,因为您通常必须在两个文件中向前看才能找到下一个匹配的行。

现在,如果您绝对(!)确定 b.txt 包含 a.txt 中的所有行,顺序与 a.txt 相同,但在不同位置插入了额外的行,那么以下 可能没问题。

顺便说一句:您的变量名称令人困惑,因此我正在重命名它们并使用 try-with-resources 来确保读者和作者被关闭。

File fileA = new File("C:\\Users\\IBM_ADMIN\\Desktop\\SAM_PUBLIC_MONTHLY_20150802\\a.txt");
File fileB = new File("C:\\Users\\IBM_ADMIN\\Desktop\\SAM_PUBLIC_MONTHLY_20150802\\b.txt");
File fileNew = new File("C:\\Users\\IBM_ADMIN\\Desktop\\SAM_PUBLIC_MONTHLY_20150802\\samnew.txt");
int count = 0;
try (Reader readerA = new BufferedReader(new FileReader(fileA));
     Reader readerB = new BufferedReader(new FileReader(fileB));
     PrintWriter writer = new PrintWriter(new FileWriter(fileNew))) {
    // Read first line of each file
    String lineA = readerA.readLine();
    String lineB = readerB.readLine();
    // Compare lines
    while (lineA != null || lineB != null) {
        if (lineB == null) {
            // Oops! Found extra line in file A
            lineA = readerA.readLine(); // Read next line from file A
        } else if (lineA == null || ! lineA.equals(lineB)) {
            // Found new line in file B
            writer.println(lineB);
            lineB = readerB.readLine(); // Read next line from file A
            count++;
        } else {
            // Lines are equal, so read next line from both files
            lineA = readerA.readLine();
            lineB = readerB.readLine();
        }
    }
}
System.out.println(count);

【讨论】:

    【解决方案2】:

    您为什么不将 b.txt 读入一个列表,然后在代码中检查您在 a.txt 中读取的每一行是否在列表中可用(list.contains())。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 1970-01-01
      相关资源
      最近更新 更多