2010-08-18 110 views
0

我有一个非常奇怪的错误: 我使用缓冲读取器(br)和写入器(bw)从一个文件读取 - 计算并写入另一个文件。 问题: 第一个文件没有完全写入新文件。最后几行会被截断。 我试着把一个打印语句看看是否正在读取文件 - 并且所有语句都打印出来了。 我做了recheck,我已经使用bw.close()java - 文件读取/写入问题

仍然没有线索。

有没有any1都有这个问题?

我的代码片段如下:

private void calculateStats(String input) throws IOException { 


    BufferedWriter out = new BufferedWriter(new FileWriter("outputstats.txt")); 
    BufferedReader br = new BufferedReader(new FileReader(input)); 
    int dtime = 0 ; 
    double ingress,inMean= 0.0; 
    double egress,outMean = 0.0; 
    String id, date, newLine = null; 
    out.write("interfaceId , I-Mean, I-STD, I-Kurt, I-Skew, E-Mean, E-STD, E-Kurt, E-Skew"+NL); 

    DescriptiveStatistics inStats = new DescriptiveStatistics(); 
    DescriptiveStatistics outStats = new DescriptiveStatistics(); 
    DescriptiveStatistics inPMean = new DescriptiveStatistics(); 
    DescriptiveStatistics outPMean = new DescriptiveStatistics(); 
    DescriptiveStatistics inPStd = new DescriptiveStatistics(); 
    DescriptiveStatistics outPStd = new DescriptiveStatistics(); 
    int j = 0; 

    while((newLine = br.readLine()) != null){ 

    // System.out.println(" new line for statistical output "+newLine); 
     StringTokenizer st = new StringTokenizer(newLine, ","); 
     for(int i = 0; i<st.countTokens(); i++){ 
      id = st.nextToken().trim(); 
      dtime = Integer.parseInt(st.nextToken()); 
      ingress = Double.parseDouble(st.nextToken().trim()); 
      egress = Double.parseDouble(st.nextToken().trim()); 
      date = st.nextToken().trim(); 

      // set the interface token for easy evaluation 

      if(interfaceId.trim().equalsIgnoreCase("no value") || !(interfaceId.trim().equalsIgnoreCase(id))){ 
       interfaceId = id; 
       if(j == 0){ 
        out.write(interfaceId + ","); 
        j = 1;//reset j value 
       }else{ 
       inMean = inStats.getMean(); 
       outMean = outStats.getMean(); 
       out.write((int) inMean + ","+(int)inStats.getStandardDeviation()+","+ 
         (int)inStats.getKurtosis()+ ","+ (int)inStats.getSkewness()+ ","+ (int)outMean + 
         ","+(int)outStats.getStandardDeviation()+","+(int)outStats.getKurtosis()+","+(int)outStats.getSkewness()+ NL); 
       inPMean.addValue(inMean); 
       inPStd.addValue(inStats.getStandardDeviation()); 
       outPMean.addValue(outMean); 
       outPStd.addValue(outStats.getStandardDeviation()); 
       out.write(interfaceId + ","); 
       inStats.clear(); 
       outStats.clear(); 
       }//end of j initialization 
      } 

      if(ingress >= 0){ 
    //    System.out.println("ingress value "+ingress); 
       inStats.addValue(ingress); 
      } 
      if(egress >= 0){ 
    //    System.out.println("egress value "+egress); 
       outStats.addValue(egress); 
      } 
     }// end of for 
    }// end of while 

    out.write((int)inMean + "," + (int)outMean); 
    out.close(); 
    br.close(); 
    percentile(inPMean,inPStd,outPMean,outPStd, "outputstats.txt"); 

} 

private void percentile(DescriptiveStatistics inPMean, 
     DescriptiveStatistics inPStd, DescriptiveStatistics outPMean, 
     DescriptiveStatistics outPStd, String inputFileName) throws IOException { 


     BufferedReader br = new BufferedReader(new FileReader(inputFileName)); 
     BufferedWriter bw = new BufferedWriter(new FileWriter("outputStatBucket.txt")); 
     String newLine = null; 
     bw.write(br.readLine()+ NL); 
     while((newLine = br.readLine())!= null){ 
      StringTokenizer st = new StringTokenizer(newLine, ","); 
      while(st.hasMoreTokens()){ 
       System.out.println("newLine "+newLine); 
          bw.write(st.nextToken()+","+calcP(st.nextToken().trim(),inPMean)+"," + calcP(st.nextToken().trim(),inPStd)+ 
         ","+st.nextToken().trim()+","+st.nextToken().trim()+","+calcP(st.nextToken().trim(),outPMean)+ 
         ","+calcP(st.nextToken().trim(),outPStd)+","+st.nextToken().trim()+","+st.nextToken().trim()+ NL); 
      } 
     } 
     bw.close(); 
     br.close(); 
} 
private int calcP(String nextToken, DescriptiveStatistics inPMean) { 
    int next = Integer.parseInt(nextToken.trim()); 
    if(next<= inPMean.getPercentile(25)){ 
     return 1; 
    }else if(next > inPMean.getPercentile(25) && next <=inPMean.getPercentile(50)){ 
     return 2; 
    }else if(next > inPMean.getPercentile(50) && next <=inPMean.getPercentile(75)){ 
     return 3; 
    }else if(next > inPMean.getPercentile(75)){ 
     return 4; 
    }else{ 
     return 0; 
    } 
} 

谢谢

+0

有是示例代码中的两个函数,它们似乎都将输入文件处理为输出文件。哪一个是问题功能? – Pointy 2010-08-18 22:00:08

+1

我强烈建议您削减您的示例代码,并删除您仍可以再现问题的所有内容。在做这件事时你可能会弄明白,如果不是这样,如果你发布了一个最简单的例子,你就更有可能获得帮助。 – 2010-08-19 02:58:03

回答

1

如果这是你得到的部分输出,可能的罪魁祸首是,你需要调用flush()确保写入写出到文件。

+2

当然调用close()会刷新缓冲区。 – Pointy 2010-08-18 21:55:59

+1

我不认为这是这里的问题,因为他关闭了文件,并且close()应该刷新缓冲区。 – 2010-08-18 21:56:12

+0

当我使用flush()时,我总是得到这个错误,我只在方法结束时使用它。不是在此之前。 java.io.IOException:流在java.io.BufferedWriter.flushBuffer(Unknown Source)处的java.io.BufferedWriter.flushBuffer(Unknown Source)在java.io.BufferedWriter.ensureOpen(Unknown Source)处关闭。 connect(DBase.java:67)at automateExport.main(automateExport.java:11)​​ – JJunior 2010-08-18 22:01:57

0

我跑这个(有一些修改),它对我来说工作正常。也许它正在写最后一行,而你正在查看outputstats.txt而不是outputStatBucket.txt。这两个名字非常相似,第一个用作第二个输入的方式有点令人困惑。

如果不是那么它的代码也不是很长,在这一点上,所以我会建议注释掉的代码的各个部分,直到仅剩下的问题(或直到它解决了)...