2012-07-25 61 views
2

所以我写这是假设写两个链表的内容到格式BufferedWriter将不会所有的数据写入到文件

“标题行1”的文本文件

“程序标题行2"

“标题行3”

X1 Y1

X2 Y2

X3 Y3

...

x1023 y1023

这里是主要写代码:

LinkedList<Double> xVal; 
    LinkedList<Double> yVal; 
    LinkedList<String> header; 
    Analize test; 
    Scanner scan = new Scanner(System.in); 
    FileWriter fstream; 
    BufferedWriter out; 

    int i=1; 
    test = new Analize(i); 
    test.addData(); 
    test.calculate(); 


    //Writing ModY 
    fstream = new FileWriter("BLOCK " +i+"_ModY.txt"); 
    out = new BufferedWriter(fstream); 
    header = test.getHeader(); 
    xVal=test.getXList(); 
    yVal=test.getYListMod(); 


    //Write Header 
    out.write(header.get(0)+"_modY");out.newLine(); 
    out.write(header.get(1));out.newLine(); 
    out.write(header.get(2));out.newLine(); 

    for(int j=0;j<xVal.size();j++) 
    { 
     double x=xVal.get(j); 
     double y=yVal.get(j); 
     out.write(x+"\t"+y+" "+j);out.newLine(); 
     //test code 
     System.out.println(xVal.get(j)+" "+yVal.get(j)+" "+j); 
     //System.out.println(j); 
    } 

的真的很烦人的事情是,测试代码行: 的System.out .println(xVal.get(j)+“”+ yVal.get(j)+“”+ j); 实际上在System.in中使用正确的值运行了1023次,但该文件只有值高达558。

我真的不知道这里发生了什么......

+4

你有没有同步,然后关闭作家和/或流? – 2012-07-25 23:20:11

回答

14

我没有看到你打电话out.close();任何地方。这将为您刷新并关闭底层流。我会在你的for循环之后把它放好。

请务必先关闭流,否则你的一些数据可能会被挂在内存等待JVM一起去和它冲洗出来给你的文件。

+0

非常感谢!我在代码的其他部分都这样做了。我无法相信在3小时左右的故障排除后我没有看到这一点。 – 2012-07-26 01:42:33