tags:

views:

61

answers:

2

hi, i m having a really weird error: i am using buffered reader (br) and writer(bw) to read from one file - do calculation and write to another file. problem: the first file does not get written to the new file completely. LAst couple of lines get truncated. I tried putting a print statement to see if the file is getting read - and all statements got printed out correctly. I did recheck that i have used bw.close()

Still no clue.

Has any1 every had this problem?

my code snippet is as follows:

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;
    }
}

Thank you,

A: 

If it's partial output you are getting, the likely culprit is that you need to call flush() to ensure writes are written out to the file.

mdma
Surely calling `close()` will flush the buffers.
Pointy
I don't think that's the problem here because he closes the file, and close() is supposed to flush the buffers.
Paul Tomblin
i always get this error when I use flush() i use it only at the end of the method. not before that. java.io.IOException: Stream closed at java.io.BufferedWriter.ensureOpen(Unknown Source) at java.io.BufferedWriter.flushBuffer(Unknown Source) at java.io.BufferedWriter.flush(Unknown Source) at DBase.connect(DBase.java:67) at automateExport.main(automateExport.java:11)
jillika iyer
@jillika iyer - the call to flush (e.g. `out.flush()`) should come before the call to close(). Although, as others have mentioned, this shouldn't be necessary here. (I've looked at the code in more detail after my initial post.)
mdma
Yes, you can't call `.flush()` if you've already called `.close()`, but there's no need to anyway. @mdma The `.close()` operation will perform the `.flush()` so it's pointless to do that.
Pointy
If it were only the last line, flush() would be worth a look. Since it's multiple lines getting truncated I'd look at the logic. Are the truncated lines being chopped in the same location? Is there an issue with StringTokenizer? Can you test it with a file that contains nothing but the last two lines of the original file?
Kelly French
please close this topic.the problem was i was using flush().i removed it and seemes to work just fine.sorry for the trouble! thanks so much :)
jillika iyer
thanks so much every1 :) have a gr8 day!
jillika iyer
A: 

I ran this (with some modifications) and it works fine for me. Maybe it's writing the final lines and you were looking at outputstats.txt instead of outputStatBucket.txt. The two names are pretty similar and it's a little confusing how the first is used as input for the second.

If that's not it then the code isn't very long at this point so I'd suggest commenting out various parts of the code until only the issue is left (or until it's solved)...

Aaron