最后一行仅在Java中部分打印

最后一行仅在Java中部分打印,java,file,file-io,wait,Java,File,File Io,Wait,我有一些比较两个文件的java代码。当它在某一行上找到类似的数字时,它会将该行打印到一个新文件中。这似乎在相当长的时间内有效。。。直到我相信这是最后一行。那一行只能部分打印出来。我认为这可能是因为代码后面出现了一个“中断”,但我在谷歌上四处查看,并不确定这些答案是否真的相关。 以下是一些我认为相关的代码: Read in files while the line isn't null... Parse files Write a header Some comparisons while (!i

我有一些比较两个文件的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;
               }

有人知道我可以添加什么来阻止最后一行突然中断吗?我知道在BASH中你可以指定它等待。。。但是我被java等价物弄糊涂了,希望有人能给我推荐一个,并帮助我更好地解释它

为了得到一个答案(直到卡尔提出他的答案),我要继续回答


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


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

为了得到一个答案(直到卡尔提出他的答案),我要继续回答


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


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

事实上,我犯了很多这样的错误,我认为这是因为使用垃圾收集器,我们并不真正关心内存管理,因此,它往往会忘记关闭我们打开的任何iostream或将内存刷新到磁盘。但这是一件可怕的事。

事实上,我犯了很多这样的错误,我认为这是因为使用垃圾收集器,我们并不真正关心内存管理,因此,它往往会忘记关闭我们打开的任何iostream或将内存刷新到磁盘。但这是一件可怕的事情。

您关闭了outputstream吗?也许你需要调用flush方法。@Carl实际上。。。我没有。。。我不知道为什么。我现在就做。Thanks@Carl这就解决了问题。这也是一件很愚蠢的事情。谢谢你的帮助。如果你把它作为答案发布,我会接受它。你关闭输出流了吗?也许你需要调用flush方法。@Carl实际上。。。我没有。。。我不知道为什么。我现在就做。Thanks@Carl这就解决了问题。这也是一件很愚蠢的事情。谢谢你的帮助。如果你把它作为答案贴出来,我会接受的。
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