1

我有一些比较两个文件的java代码。当它在特定行上找到相似的数字时,它会将该行打印到一个新文件中。这似乎工作了很长时间......直到我相信是最后一行。那条线只会被部分打印出来。我认为这可能是因为代码后面的“中断”,但我在谷歌上环顾四周,并不确定是否有任何答案真的相关。这是一些我认为相关的代码:

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 等价物感到困惑,并希望有人可以为我推荐一个并帮助更好地解释它。

4

2 回答 2

4

为了得到答案(直到卡尔提出他的答案)我将继续回答

你关闭了输出流吗?也许你需要调用flush方法。– 卡尔

他是对的。我没有。傻我。

于 2013-04-10T17:44:05.400 回答
1

Actually, i made that mistakes a lot, i think it is because with the garbage collector,we don't really care about memory management,so,it tends to forget to close any iostream we'd opened or to flush memory to disk.That's a terrible thing to do though.

于 2013-04-11T02:13:38.013 回答