我试图逐行比较2个文件。在进行这种比较之后,所有行都被复制到第三个文件,它们被标识为"出现在两个文件中#34 ;; "不包括fileA&#34 ;;或"不包括fileB"。感谢@nono和@Andreas的帮助,我的代码正在运行,但输出文件上还没有写入任何内容。
public static void compareFileLines(String fileLocA, String fileLocB, String fileLocC) throws IOException{
//fileLoc'X' is the filepath
try{
BufferedReader reader1 = new BufferedReader( new FileReader(fileLocA));
BufferedReader reader2 = new BufferedReader( new FileReader(fileLocB));
PrintWriter writer = new PrintWriter(new File(fileLocC));
String []linesA = new String [fileLenght(fileLocA)]; //filelenght checks number of lines on a file
String []linesB = new String [fileLenght(fileLocB)];
for (int i=0; i< linesA.length-1; i++){
linesA[i]= reader1.readLine();
}
for (int i=0; i< linesB.length-1; i++){
linesB[i]= reader2.readLine();
}
boolean finder = false;
for (int n =0; n<linesA.length-1; n++){
for(int m=0; m<linesB.length-1; m++){
if (linesA[n].equals(linesB[m])){
finder = true;
writer.append(linesA[n]+" Is in both files \n");
}
if (finder == false){
writer.append(linesA[n]+ "Is exclusive of File1");
}
finder = false;
}
}
finder = false;
for (int n=0; n< linesB.length -1; n++){
for (int m=0; m< linesA.length -1; m++){
if (linesB[n].equals(linesA[m])){
finder = true;
}
}
if (finder == false){
writer.append(linesB[n] +" is exclusive of File2");
}
finder = false;
}
}finally{
}
}
答案 0 :(得分:0)
BufferedReader构造函数可以接受FilReader。 FileReader可以接受文件或路径。
FileReader fr = new FileReader(&#34; text.txt&#34;);
BufferedReader br = new BufferedReader(fr);
循环使用: 字符串s; while((s = br.readLine())!= null){ ... }