我正在合并两个文本文件,分别是123456
和abcdefg
。我遇到的现象是在同一行创建的23456abcde
文件,正如我在此处输入的那样。
我的问题是
1
没有被写入
public static String mergeRecords(String in1, String in2, String out) {
BufferedReader br1 = null;
BufferedReader br2 = null;
BufferedWriter bw1 = null;
try{
FileReader fr1 = new FileReader(in1);
FileReader fr2 = new FileReader(in2);
br1 = new BufferedReader(fr1);
br2 = new BufferedReader(fr2);
bw1 = new BufferedWriter(new FileWriter(out));
////File Writing
while(!(br1.readLine()==null)||(br2.readLine()==null)){
//alternating between the two filles
if((f%2)==0){
bw1.write(br1.read());
bw1.write(br1.readLine());
bw1.write(br1.readLine());
bw1.write(br1.readLine());
bw1.write(br1.readLine());
bw1.write(br1.readLine());
}
else{
bw1.write(br2.read());
bw1.write(br2.readLine());
bw1.write(br2.readLine());
bw1.write(br2.readLine());
bw1.write(br2.readLine());
bw1.write(br2.readLine());
}
/////
//File ALternator Value
f++;
}
bw1.close();
}
catch(IOException iox){
}
return "'mergeRecords' not yet implemented";
}
答案 0 :(得分:3)
问题在于:
while(!(br1.readLine()==null)||(br2.readLine()==null)){
消耗第一行br1。它没有评估第二个语句,因为第一个语句已经是真的。你永远不会对那条线做任何事情,所以它就丢失了。