gpt4 book ai didi

java - 如何比较两个文本文件的内容并返回 "Same Content"或 "Different Content"?

转载 作者:行者123 更新时间:2023-11-29 09:46:36 31 4
gpt4 key购买 nike

<分区>

我的 Java 应用程序需要能够比较文件系统中的两个不同文件,并确定它们的二进制内容是否相同。

这是我当前的代码:

package utils;
import java.io.*;

class compare {
public static void main(String args[]) throws IOException {
FileInputStream file1 = new InputStream(args[0]);
FileInputStream file2 = new InputStream(args[1]);

try {
if(args.length != 2)
throw (new RuntimeException("Usage : java compare <filetoread> <filetoread>"));
while (true) {
int a = file1.read();
int b = file2.read();
if (a==-1) {
System.out.println("Both the files have same content");
}
else{
System.out.println("Contents are different");
}
}
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}
}

任何有关如何正确进行比较功能的提示或建议都将不胜感激。

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com