[英]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.