[英]Last line only partially prints in Java
我有一些比较两个文件的Java代码。 当在特定的行上找到相似的数字时,它将把该行打印到新文件中。 这似乎很有效,直到我相信最后一行。 该行只会被部分打印。 我认为这可能是由于代码后面的“中断”所致,但是我在google上四处浏览,不确定是否有任何答案确实相关。 这是我认为相关的一些代码:
Read in files
while the line isn't null...
Parse files
Write a header
Some comparisons
while (!input.startsWith("#") && !input.startsWith("P")) {
prse = input.split("\t");//split the file by tabs
pos = prse[7];
poson = prse[8];
pos = Integer.parseInt(poson);
if (cnt < num.size()) { //if we haven't exceeded an array
if (num.get(cnt).equals(pos)) { //if the first number is the same
if (cnt2 < posstart.size()) { //if we haven't exceeded another array
end = Integer.parseInt(posend.get(cnt2)); //change to int
start = Integer.parseInt(posstart.get(cnt2));//change to int
if (pos < start) { //if it is less then the starting pos then it can't fall within
break; //so break
}
if (pos < end && pos > start) {//I am trying to see if a number falls within the range of numbers from a separate file
out1.write(input + "\n"); //If it does: This is where I am writing out the line
break; //if I remove this break the program hangs here
} else {
cnt2++; //if it wasn't the same, add
}
}
} else {
cnt++; //if it was the same move to the next one
cnt2 = 0; //reset this number
break; //go back to beginning
}
} else {
break;
}
因此,该代码可以完美地运行约6500行,但随后突然中断了最后一行:
Blah B 6 5 8 C 5 X 6
Blah A 0 1 4 C 2 X 7
Blah B 3 5 9 C 5 X 6
Blah B 0 9 4
有谁知道我可以添加些什么来阻止最后一条线突然中断? 我知道在BASH中您可以指定要等待...但是我对Java等效项感到困惑,并希望有人可以为我提出一个建议并帮助对其进行更好的解释。
为了得到答案(直到卡尔提出他的答案),我将继续回答
您关闭输出流了吗? 也许您需要调用flush方法。 –卡尔
他是对的。 我没有 傻我
实际上,我犯了很多错误,我认为这是因为使用垃圾收集器时,我们实际上并不关心内存管理,因此,它往往会忘记关闭我们打开的任何iostream或将内存刷新到磁盘。但这是一件可怕的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.