我想比较2个文件并检查内容是否相等。但是问题是文件中的数据顺序不同。当元素未按相同顺序放置时,即使内容相等,equals()方法也会返回false。如何通过忽略顺序在Java中比较这些文件?
最好的祝福!
最佳答案
这是您想要的代码:
Scanner input1= new Scanner(new File("C:/file1.txt");
Scanner input2= new Scanner(new File("C:/file2.txt");
String one= input1.nextLine();//assuming files contain only one line
String two= input2.nextLine();//assuming files contain only one line
Set<String> set1 = new HashSet<String>(Arrays.asList(one.split(";"));
Set<String> set2 = new HashSet<String>(Arrays.asList(two.split(";"));
System.out.println(set1.equals(set2));
关于java - 当元素的顺序无关紧要时如何在Java中比较两个文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24408481/