2010-08-18 142 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。這兩個名字非常相似,第一個用作第二個輸入的方式有點令人困惑。

如果不是那麼它的代碼也不是很長,在這一點上,所以我會建議註釋掉的代碼的各個部分,直到僅剩下的問題(或直到它解決了)...