java - 将第一个文件的每一行与java中第二个文件的全部内容进行比较

标签 java file compare

我想将第一个文件的每一行与第二个文件的全部内容进行比较并打印匹配结果。 我正在使用这段代码,但它进入了其他部分并且没有给我结果。 我的文件是这样的

file1 - store.txt               file2 - data.txt    

1,CNB,1234                       1001,1234
2,NCD,1567                       1002,1345
3,ABC,1111                       1003,1111
etc etc                          etc etc

我想要的结果是

CNB, 1234
ABC, 1111

这是我编写的代码:

String y = ",", z = ";";

        File file1 = new File("store.txt");
        File file2 = new File("data.txt");

        BufferedReader bfr = new BufferedReader(new FileReader(file1));
        BufferedReader bfr1 = new BufferedReader(new FileReader(file2));

        String name1;
        String name2;
        String[] f1, f2;

        try{

        while (bfr1.readLine() != null && bfr.readLine() != null)
        {

            name1 = bfr.readLine();
            name2 = bfr1.readLine();

            f1 = name1.split(y);
            f2 = name2.split(z);


            if (f1[1].equals(f2[1]))
            {
                //System.out.println(f1[0] + f1[1]);
                for(int i=0; i < 1000;i++) {
                    name1 = bfr.readLine();

                name2 = bfr1.readLine();

                 System.out.println(f1[0] + " \t here");
                }

                //System.out.println(f1[0] + " \t here");
               // System.out.println(s1);
            }

            else
            {
            System.out.println("Not equal");
            }
        }

    }
        catch (Exception e)
        {
            e.printStackTrace();
        }
}
}

谢谢大家的帮助! :)

最佳答案

您的代码可以进行很多优化,因为正如其他海报所说,您的嵌套迭代无需跟踪先前的迭代。您需要的“行获取”数量随着行数的增加而指数级增长,这确实一点都不好。

还有一个逻辑错误,因为你在这个比较中犯了错误f1[1].equals(f2[1]);根据您的预期结果,我假设您想要比较 f1[2].equals(f2[1])

这里的好消息是,您可以使用两个 HashMap 来存储两次迭代的结果,然后只需迭代它们一次即可获得结果。

这是代码,我没有测试过,但它应该可以工作:

private void compareFiles() {
        BufferedReader bfr1 = null;
        BufferedReader bfr = null;
        String split = ",";
        HashMap<String, String> fileCache1 = new HashMap<String, String>();
        HashMap<String, String> fileCache2 = new HashMap<String, String>();
        try {
            File file1 = new File("store.txt");
            File file2 = new File("data.txt");
            bfr = new BufferedReader(new FileReader(file1));
            bfr1 = new BufferedReader(new FileReader(file2));

            String name1;
            String name2;
            String[] f1;
            String[] f2;
            try {
                // read the first file and store it in an Hashmap (first column ignored)
                while (bfr.readLine() != null) {
                    name1 = bfr.readLine();
                    f1 = name1.split(split);
                    fileCache1.put(f1[2], f1[1]);
                }
                // read the second file and store it in an Hashmap (second column useless?)
                while (bfr1.readLine() != null) {
                    name2 = bfr1.readLine();
                    f2 = name2.split(split);
                    fileCache2.put(f2[1], f2[0]); // f2[0] is useless i think
                }

                // iterate once over the first hashmap and compare with the second.
                for (String key1 : fileCache1.values()){
                    if(fileCache2.containsKey(key1)){ // here is your desired match
                        System.out.println(fileCache1.get(key1)+", "+key1); // print or do whatever you want
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(SO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                bfr1.close();
            } catch (IOException ex) {
                Logger.getLogger(SO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

关于java - 将第一个文件的每一行与java中第二个文件的全部内容进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21474424/

相关文章:

linux - 查找不属于特定用户的文件

arrays - 珀尔。比较具有重复值的数组

python - 比较两个列表并获得数字差异

java - 如何在 JButton ImageIcon 中使颜色不可见(透明)

c# - 如何关闭已在目录中打开的文件

java - 使用 JNativeHook 制作游戏

windows - 二进制将所有文件与指定目录(和子目录)中的所有文件进行比较

javascript - 比较对象数组中的项目

java - 如何通过 Java 中的通用通配符捕获创建数组的克隆?

java - 原始包装器内存空间开销与泛型,在 Java 中