我现在用的方法是:
    fr=new FileReader(path+"\\"+xmlname+".xml");
    br=new BufferedReader(fr);
    Line=br.readLine();
    int linNumber = 0;
    for(;Line!=null;linNumber++){
          Line=br.readLine();
    }    这对于小文件来说还可以,但是如果文件比较大的话,效率就会很低,不知道还有没有其他更好的方法,还请各位指教,谢谢!!

解决方案 »

  1.   

    如果是Unix系统,调用本地命令wc -l读取返回值;
    如果是Windows,没有什么好的办法。
      

  2.   


      谢谢各位的指教!
      虽然我还没有找到好一点的方法。  healer_kx(不会C的程序员那也算程序员?----甘草) 
      LineReader好象不是读行数的吧,愿闻其详。
      

  3.   

    这样试试
    我有一个小例子 你看看吧!
    ===========================================================================
    import java.io.*;
    public class file{
    /*fileRead是将提供的一个参数filepath(文件路径)的文件内容读入页面
     *@param  filepath是String 类型
     *@return fileRead返回filepath指定文件的内容
     */
    public String fileRead(String filepath){
    String content="";
    try{
    File f=new File(filepath);
    BufferedReader br=new BufferedReader(new FileReader(f));
    String temp=null;
    int line=0;
      while ((temp=br.readLine())!=null){
       line++;
       content+=temp+"\n";
      }
      System.out.println("一共有:"+line+"行");
    }catch(FileNotFoundException e){
      System.out.println("文件没有找到!");
    }catch(IOException e){
      System.out.println("IO读写错误!");
    }
     return content;
    } public static void main(String[] args){
    file f =new file();
    System.out.println(f.fileRead("C:\\tomcat5\\webapps\\ROOT\\RELEASE-NOTES.txt"));
    }
    }
      

  4.   

    应该直接用inputStream都换行符 不要去readline 应该会快一点的
      

  5.   

    应该直接用inputStream读换行符 不要去readline 应该会快一点的
      

  6.   

    直接使用java nio,将文件映射进内存,是效率最高的
      

  7.   

    直接以字符流读入
    然后用indexof("\n")来统计回车符不是更好
      

  8.   

    File file = new File("a.txt");
    FileReader fr = new FileReader(file);char[] buf = new char[1];
    int read = 0;int count = 0;while((read=fr.read(buf))!=-1){
      if(buf[0].equals("\n")) count++;
    }想了一下 直接遍历字符不就行了
      

  9.   

    看了一个opensolaris的wc的实现代码,就是直接遍历字符然后统计character,lines,words.所不同的是他使用了缓冲区,即不是一次性读入,而是一次性读入4K.
      

  10.   

    http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/cmd/wc/wc.c
      

  11.   

    用InputStream,可以读取一定长度,如4k,然后查找'\n'。提高速度可以使用BufferedInputStream
    但绝不可以把文件全读出来,或者使用readLine,这两者都可能造成OutofMemory错误。