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

时间:2021-02-07 21:49:54

Hy, In the result, the number of zeros and ones isn't the same.

Hy,结果中,0和1的数量不一样。

And i don't find where is the problem.

我找不到问题所在。

Can any One help me please?

任何人都可以帮助我吗?

        //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");

    }

}

//Output

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

Thank you, Best Reagrds

谢谢你,Best Reagrds

2 个解决方案

#1


The problem lies in the code here:

问题出在这里的代码:

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() actually reads one character. Instead of processing that character, you discard the result by reading the remaining whole line by br.readline().

br.read()实际上读取一个字符。您可以通过br.readline()读取剩余的整行来放弃结果,而不是处理该字符。

Since there are 1000 lines in the file and you discarded the first character of each line, you end up 1000 characters less.

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

You may change your for(int i=0; (i=br.read())>-1;) to while (br.ready()). When the br is empty, the loop will terminate

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

#2


You're generating 0 and 1 values using Random class.

您使用Random类生成0和1值。

This class generates a random distribution so it doesn't ensures that the number of 0 and 1 characters generated will be the same, because it's a random generator.

此类生成随机分布,因此不能确保生成的0和1个字符的数量相同,因为它是随机生成器。

Only if you generate an infinite amount of numbers then there will be the same number of 0s and 1s.

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

The more characters you generate, the closer the two values will be.

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

#1


The problem lies in the code here:

问题出在这里的代码:

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() actually reads one character. Instead of processing that character, you discard the result by reading the remaining whole line by br.readline().

br.read()实际上读取一个字符。您可以通过br.readline()读取剩余的整行来放弃结果,而不是处理该字符。

Since there are 1000 lines in the file and you discarded the first character of each line, you end up 1000 characters less.

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

You may change your for(int i=0; (i=br.read())>-1;) to while (br.ready()). When the br is empty, the loop will terminate

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

#2


You're generating 0 and 1 values using Random class.

您使用Random类生成0和1值。

This class generates a random distribution so it doesn't ensures that the number of 0 and 1 characters generated will be the same, because it's a random generator.

此类生成随机分布,因此不能确保生成的0和1个字符的数量相同,因为它是随机生成器。

Only if you generate an infinite amount of numbers then there will be the same number of 0s and 1s.

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

The more characters you generate, the closer the two values will be.

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