统计/n数量?

解决方案 »

  1.   

    似乎读取文件就是用一行一行的读的,听说从文件读到缓冲区可能不是一行一行的读,但 readLine()就是一行一行的读文件的啊。读几次就是几行了
    InputStream is=new FileInputStream("");
    BufferedReader br=new BufferedReader(new InputStreamReader(is));String s;
    while((s=br.readLine())!=null){
    System.out.println(s);
    }
      

  2.   

    IO流中有个按行读的,你去看一下API,自己写个计数就可以了, public static void main(String[] args) {
    String str = "";
    int n = 0;
    File ft = new File("E:\\temp\\comic.sql");
    StringBuffer sb = new StringBuffer();
    try {
    InputStream is = new FileInputStream(ft);
    BufferedReader in = new BufferedReader(new InputStreamReader(is,
    "GBK"));
    while (null != (str = in.readLine())) {
    n ++;
    }
    } catch (Exception e) {
    System.out.println("读数据错误");
    }
    }
      

  3.   

    楼上的通过验证:)完整代码如下,需要在程序所在目录保存hello.txt文件(即想得到行数的文件)
    import java.io.*;
    class Test{
        public static void main(String[] args) {
            String str = "";
            int n = 0;
    String path = new File("").getAbsolutePath();
            File ft = new File(path + "\\hello.txt");
            StringBuffer sb = new StringBuffer();
            try {
                InputStream is = new FileInputStream(ft);
                BufferedReader in = new BufferedReader(new InputStreamReader(is,
                        "GBK"));
                while (null != (str = in.readLine())) {
                    n ++;
                }
            } catch (Exception e) {
                System.out.println("读数据错误");
            }
    System.out.println(n);
        }
    }