1 java
我的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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
关于如何正确地进行比较功能的任何提示或建议将不胜感激.
最简单的方法是将内容读入两个字符串,例如
FileInputStream fin = new FileInputStream(args[i]);
BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
StringBuilder sb = new StringBuilder();
while ((thisLine = myInput.readLine()) != null) {
sb.append(thisLine);
}
Run Code Online (Sandbox Code Playgroud)
,并执行.equals()这些.您需要更复杂的差异功能吗?
| 归档时间: |
|
| 查看次数: |
53745 次 |
| 最近记录: |