简体   繁体   English

java-文件读/写问题

[英]java - file read/write problem

im 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. 即时消息有一个很奇怪的错误:我正在使用缓冲的读取器(br)和writer(bw)从一个文件读取-做计算并写入另一个文件。 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() 我确实检查过我是否使用过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, 谢谢,

如果您得到的是部分输出,则可能的罪魁祸首是您需要调用flush()来确保将写操作写出到文件中。

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. 也许是在写最后几行,而您正在查看的是outputstats.txt而不是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)... 如果不是这样,那么此时的代码还不是很长,所以我建议注释掉代码的各个部分,直到只剩下问题为止(或直到​​问题解决为止)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM