[英]BufferedWriter not Writing First Line of Text
I am merging two text files with 123456
and abcdefg
on their own lines respectively. 我将两个文本文件分别与123456
和abcdefg
合并在一起。 The phenomena I am experiencing is a file that is created that is 23456abcde
on the same line, so exactly as i have it typed here. 我遇到的现象是在同一行上创建的文件为23456abcde
,因此与我在此处键入的完全相同。
My question is 我的问题是
1
from the first file not being written 为什么第一个文件中的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";
}
The problem is that: 问题是:
while(!(br1.readLine()==null)||(br2.readLine()==null)){
consumes the first line of br1. 消耗br1的第一行。 It doesn't evaluate the second statement because the first one is already true. 它不会评估第二条语句,因为第一条已经是正确的。 You never do anything with that line, so it's lost. 您永远不会对那条线做任何事情,所以就迷路了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.