static void read1() throws IOException {
    
            // read bytes from the file one at a time
    
            MyTimer mt = new MyTimer();
            FileInputStream fis = 
                         new FileInputStream(TESTFILE);
            cnt1 = 0;
            int c;
            while ((c = fis.read()) != -1) {
                if (c == 'A') {
                    cnt1++;
                }
            }
            fis.close();
            System.out.println("read1 time = " + 
                                      mt.getElapsed());
        }
    
        static void read2() throws IOException {
    
            // read bytes from the file one at a time 
            // with buffering
    
            MyTimer mt = new MyTimer();
            FileInputStream fis = 
                         new FileInputStream(TESTFILE);
            BufferedInputStream bis = 
                          new BufferedInputStream(fis);
            cnt2 = 0;
            int c;
            while ((c = bis.read()) != -1) {
                if (c == 'A') {
                    cnt2++;
                }
            }
            fis.close();
            System.out.println("read2 time = " + 
                                      mt.getElapsed());
        }
    
        static void read3() throws IOException {
    
            // read from the file with buffering
            // and with direct access to the buffer
    
            MyTimer mt = new MyTimer();
            FileInputStream fis = 
                         new FileInputStream(TESTFILE);
            cnt3 = 0;
            final int BUFSIZE = 1024;
            byte buf[] = new byte[BUFSIZE];
            int len;
            while ((len = fis.read(buf)) != -1) {
                for (int i = 0; i < len; i++) {
                    if (buf[i] == 'A') {
                        cnt3++;
                    }
                }
            }
            fis.close();
            System.out.println("read3 time = " 
                                    + mt.getElapsed());
        }

解决方案 »

  1.   

    read():读一个字符,如果到达stream末尾,则返回-1
    readLine():是BufferedReader的方法,读一个以'\r'或'\n'结尾的串,如果到达末尾,则返回null
      

  2.   

    同意Veeve() :
    read():读一个字符,如果到达stream末尾,则返回-1
    readLine():是BufferedReader的方法,读一个以'\r'或'\n'结尾的串,如果到达末尾,则返回null补充:我们可以单从意思上理解,readline,就是都一行,而每一行的结尾一般都是以"\n"结束的===================================
             情人节快乐
            有情人终成眷属
       我的一分耕耘,你能给一分收获
      

  3.   

    read返回的是int,readLine返回的是String
      

  4.   

    “readLine():是BufferedReader的方法,读一个以'\r'或'\n'结尾的串”那么readLine()把字符'\r'也都进去了吗?如果readLine()以后,再进行readchar(),那么会不会读到字符'\r'???还是读到字符'\r'后面的字符?