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

Java 计算给定文件中的特殊字符数,java,java.util.scanner,Java,Java.util.scanner,Hy, 结果,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");

Hy, 结果,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");

    }

}
谢谢,,
最佳结果

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

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

只有当生成无限多的数字时,才会有相同数量的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.readline读取剩余的整行来丢弃结果

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


你可以改变你的forint i=0;i=br.read>-1;准备好了。br为空时,循环将终止

Ah好的,是的,可以工作。因此问题不在Scanner类中。非常感谢你:
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++;
        }
    }
}