统计文本有多少行的java代码

import java.io.*;

public class LowStac {

    public static void main(String[] args) {
        String pathname = "D:\\2.txt";
        tongji(pathname);     //调用去重复的方法...
    }
    public static void tongji(String pathname) {
        
        try{   
            File file = new File(pathname);//读取文件
            BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
            BufferedReader buffer = new BufferedReader(new InputStreamReader(fis,"gbk"),20*1024*1024);//设缓存区
            String temp = ""; // 临时字符串
            int x = 0;
            while ((temp = buffer.readLine()) != null) { // 读文件,一行读一个
                x++;
                }
            System.out.println("行= "+x);
            fis.close();
        }catch(Exception e){
            System.out.println("error") ;
        }
       
    }
}
用于文本统计有多少行

你可能感兴趣的:(统计文本有多少行的java代码)