计算给定文件中

时间:2015-05-04 16:58:09

标签: java java.util.scanner

HY, 结果中,零和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

谢谢你, 最佳Reagrds

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

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().

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

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