我创建了一个java程序,它将比较两个文件,其中两个文件由新文件和旧文件组成。见下文:
new files --> new.txt
postgresql-contrib-8.1.23-10.el5_10.x86_64.rpm
postgresql-devel-8.1.23-10.el5_10.i386.rpm
postgresql-devel-8.1.23-10.el5_10.x86_64.rpm
postgresql-docs-8.1.23-10.el5_10.x86_64.rpm
postgresql-libs-8.1.23-10.el5_10.i386.rpm
tzdata-2014b-1.el5.x86_64.rpm
tzdata-java-2014b-1.el5.x86_64.rpm
xalan-j2-2.7.0-6jpp.2.x86_64.rpm
xalan-j2-demo-2.7.0-6jpp.2.x86_64.rpm
xalan-j2-javadoc-2.7.0-6jpp.2.x86_64.rpm
xalan-j2-manual-2.7.0-6jpp.2.x86_64.rpm
xalan-j2-xsltc-2.7.0-6jpp.2.x86_64.rpm
xorg-x11-server-sdk-1.1.1-48.101.el5_10.3.x86_64.rpm
xorg-x11-server-Xdmx-1.1.1-48.101.el5_10.3.x86_64.rpm
old files --> old.txt
xalan-j2-2.6.0-6jpp.2.x86_64.rpm
xalan-j2-demo-2.6.0-6jpp.2.x86_64.rpm
xalan-j2-javadoc-2.6.0-6jpp.2.x86_64.rpm
xalan-j2-manual-2.6.0-6jpp.2.x86_64.rpm
xalan-j2-xsltc-2.6.0-6jpp.2.x86_64.rpm
xorg-x11-server-sdk-1.1.1-48.101.el5_10.3.x86_64.rpm
xorg-x11-server-Xdmx-1.1.1-48.101.el5_10.3.x86_64.rpm
对于上面给出的文件,我的问题是如何逐字符串地比较它是旧的,哪些是新的,然后删除旧版本,用新的替换它。必须删除所有旧文件并将其替换为所有新文件。
示例代码。它只是比较获取和删除文件尚未更新的文件。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Testing {
public static void main(String[] args) throws IOException {
BufferedReader bfr2 = new BufferedReader(new InputStreamReader(
System.in));
String s1 = "";
String s3 = "";
String y = "", z = "";
File file1 = new File("C:\\Users\\geno\\Desktop\\newfiles.txt");
File file2 = new File("C:\\Users\\geno\\Desktop\\oldfiles.txt");
// File file1 = new File("C:\\Users\\geno\\Desktop\\changes.txt");
//File file2 = new File("C:\\Users\\geno\\Desktop\\copy.txt");
BufferedReader bfr = new BufferedReader(new FileReader(file1));
BufferedReader bfr1 = new BufferedReader(new FileReader(file2));
while ((z = bfr1.readLine()) != null)
s3 += z;
while ((y = bfr.readLine()) != null)
s1 += y;
System.out.println();
System.out.println(s3);
if (s3.equals(s1)) {
System.out.println("Content of both files are same");
} else {
System.out.println("Content of both files are not same");
}
}
}`