Advertisement

统计和输出非空行内容

阅读量:

1、功能概述?

在开发过程中,除了需要读取文件内容之外,有时还需要统计文件中非空行的数量。

获取这一信息的方式存在多种选择,其中部分方法操作较为繁琐,而另一些方法则相对简单快捷。

2、文件行数统计办法1?

第一种途径在JDK版本方面不存在约束,然而实际应用过程中存在操作不够便捷的问题,主要源于其整体表现缺乏一定的技术先进性。

复制代码
 public static void main(String[] args) {

    
     String filePath = "C://file.txt";
    
     int lineCount = 0;
    
     try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
    
         String line;
    
         while ((line = reader.readLine()) != null) {
    
             if (!line.trim().isEmpty()) {
    
                 lineCount++;
    
             }
    
         }
    

全部评论 (0)

还没有任何评论哟~