我正在做一些阅读文件的练习,我的任务是为不平衡文件编写File Interleave代码.所以如果我有这样设置的东西它工作正常:
while ((inputLine1 = LineSequential.read(inFileStreamName1)) != null **&&** (inputLine2 = LineSequential.read(inFileStreamName2)) != null)
请注意&& …但如果我将其更改为|| netbeans声明inputLine2尚未初始化(尽管它以这种方式看到inputLine1非常精细):
while ((inputLine1 = LineSequential.read(inFileStreamName1)) != null **||** (inputLine2 = LineSequential.read(inFileStreamName2)) != null)
现在,如果inputLine1(这是来自文件A的数据)首先变为空,我只得到inputLine2中的每一行(来自文件B).在每次循环迭代中,它跳过一条线.我很确定它与我初始化inputLine2的位置有关..:
while ((inputLine1 = LineSequential.read(inFileStreamName1)) != null || LineSequential.read(inFileStreamName2) != null) {
inputLine2 = LineSequential.read(inFileStreamName2);
System.out.println(inputLine2);
if(inputLine1 != null && inputLine2 == null){
LineSequential.write(outFileStreamName, inputLine1);
}else if(inputLine1 == null && inputLine2 != null){
LineSequential.write(outFileStreamName, inputLine2);
}
else if(inputLine1 != null && inputLine2 != null){
LineSequential.write(outFileStreamName, inputLine1);
LineSequential.write(outFileStreamName, inputLine2);
}
}