计算给定文件中的特殊字符数

Counting number of special characters in a given file

嗨, 结果,0 和 1 的个数不一样。

我也没找到问题出在哪里。

谁能帮帮我吗?

        //Main {
        int bufferSize = 10240; //10KB
        int fileSize = 10 * 1024 * 1024; //10MB
        Random r = new Random();

        //Writing 0 and 1 into file
        File file = new File("test.txt");
        FileWriter fw = new FileWriter(file, false); //this false means, every time we want to write into file, it will destructs what was before
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter pw = new PrintWriter(bw);

        for(int i=0; i<1000; i++){
            for(int j=0; j<1000; j++){
                if(r.nextBoolean()){
                    pw.write("0 ");
                }else{
                    pw.write("1 ");
                }
            }
            pw.write("\n");
        }

        System.out.println("End of writing into file : " + file.getName() + ", in : " + file.getAbsolutePath() + ", and its size : " + file.length());
        pw.close();


        //Read from file, and counting number of zeros and ones
        System.out.println("Reading from file : Scanner method");
        Scanner sc = null;
        //sc = new Scanner(new BufferedReader(new FileReader(file)));
        sc = new Scanner(new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), bufferSize));
        int countZeros=0;
        int countOnes=0;
        StringTokenizer st = null;
        String temp = null;

        //Start counting time
        long debut  = System.nanoTime();
        while(sc.hasNext()){
            st = new StringTokenizer(sc.next(), " ");
            while(st.hasMoreTokens() ){
                temp = st.nextToken();
                if(temp.compareTo("0")==0 && !Character.isSpaceChar(temp.charAt(0))){
                    countZeros++;
                }
                else if(temp.compareTo("1")==0 && !Character.isSpaceChar(temp.charAt(0))){
                    countOnes++;
                }
            }
        }
        //End counting time
        long end  = System.nanoTime() - debut;
        sc.close();
        System.out.println("Number of Zeros : " + countZeros);
        System.out.println("Number of Ones : " + countOnes);
        System.out.println("Total of zeros and Ones : " + (countZeros+countOnes));
        System.out.println("Duration of counting zeros and ones : " + end/1000000 + "ms");


        System.out.println("************");
        System.out.println("Reading from file : BufferedReader method");

        countZeros=0;
        countOnes=0;
        st=null;
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), bufferSize);
        String[] tempLigne = null;
        //Start counting time
        debut  = System.nanoTime();
        for(int i=0; (i=br.read())>-1;){
            tempLigne = br.readLine().split(" ");
            for(int j=0; j<tempLigne.length; j++){
                if(tempLigne[j].equals("0")){
                    countZeros++;
                }else if(tempLigne[j].equals("1")){
                    countOnes++;
                }
            }
        }
        //End counting time
        end  = System.nanoTime() - debut;
        br.close();
        System.out.println("Number of Zeros : " + countZeros);
        System.out.println("Number of Ones : " +  countOnes);
        System.out.println("Total of zeros and Ones : " + (countZeros+countOnes));
        System.out.println("Duration of counting zeros and ones : " + end/1000000 + "ms");

    }

}

//输出

End of writing into file : test.txt, in : C:\Users\youness\workspace\ScannerFile\test.txt, and its size : 1990656
Reading from file : Scanner method
Number of Zeros : 499807
Number of Ones : 500193
Total of zeros and Ones : 1000000
Duration of counting zeros and ones : 1020ms
************
Reading from file : BufferedReader method
Number of Zeros : 499303
Number of Ones : 499697
Total of zeros and Ones : 999000
Duration of counting zeros and ones : 177ms

谢谢, 最佳成绩

您正在使用随机生成 0 和 1 值 class。

这个 class 生成一个随机分布,所以它不能确保生成的 0 和 1 字符的数量相同,因为它是一个 random 生成器.

只有生成无限多的数字,才会有相同数量的0和1。

您生成的字符越多,这两个值就会越接近。

问题出在代码这里:

for(int i=0; (i=br.read())>-1;){
    tempLigne = br.readLine().split(" ");
    for(int j=0; j<tempLigne.length; j++){
        if(tempLigne[j].equals("0")){
            countZeros++;
        }else if(tempLigne[j].equals("1")){
            countOnes++;
        }
    }
}

br.read() 实际上 reads one character。您不处理该字符,而是通过 br.readline().

读取剩余的整行来丢弃结果

由于文件中有 1000 行并且您丢弃了每行的第一个字符,因此最终少了 1000 个字符。

您可以将 for(int i=0; (i=br.read())>-1;) 更改为 while (br.ready())。当br为空时,循环终止