自己问的不清楚嘛。比如一个二进制文件,你拿什么判断它的行数。
比如下面的文件(od -x 显示):
===============================================
0000000 2749 6d61 6120 6620 6c69 2e65 490a 6127
0000020 206d 2061 6966 656c 0a2e
0000032
===============================================
你说有多少行?

解决方案 »

  1.   

    呵呵,那除了一行一行读取计数之外,你说还有什么好办法?
    知道UNIX下的wc命令吗?也是这样的方法实现的。
      

  2.   

    回车字符有 0D, 0A, 0D0A
    只能一个一个字符读过来,在程序中计数。
      

  3.   

    int countTextFileLines(String fileName) {
        int count = 0;
        File f = new File(fileName);
        if (f.exists() == false || f.isDirectory() || f.canRead() == false) {
          return count;
        }    boolean fileEmpty = true;
        try {
          BufferedReader reader = new BufferedReader(new FileReader(f));
          int c;
          boolean lastCharIs0D = false;
          while ( (c = reader.read()) > -1) {
            fileEmpty = false;
            System.out.print( (char) c);
            if (c == 0x0A) {
              if (lastCharIs0D == false) {
                count++;
              }
              lastCharIs0D = false;
            }
            else if (c == 0x0D) { //count add 1 when 0D or 0D0A
              count++;
              lastCharIs0D = true;
            }
            else {
              lastCharIs0D = false;
            }
          }
          reader.close();
        }
        catch (Exception e) {
          e.printStackTrace();
          return 0;
        }
        if (fileEmpty == false) {
          count++;
        }    return count;
      }
      

  4.   

    readLine()里边进行计数(外面循环)
    也许速度不行
      

  5.   

    可能上面的代码有一个小bug, 如果最后的字符为换行,多计算了一行。