java - 计算文本中跳过空行的行数

标签 java

我需要这方面的帮助。你能告诉我如何在不计算空白行的情况下计算 input.txt 中的行数吗?

到目前为止,我尝试过:

    BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
    int lines = 0;
    while (reader.readLine() != null)
    lines++;

所以,这段代码能够计算行数,但是有空行!我知道有字符/n 说明新行,但我不知道如何将它集成到解决方案中。

我也尝试计算行数、空行数并减去它们,但我没有成功。

最佳答案

BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
int lines = 0;
String line;
while ((line = reader.readLine()) != null){
    if(!"".equals(line.trim())){
        lines++;
    }
}

关于java - 计算文本中跳过空行的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20083936/

相关文章:

java - 增强for循环中的类方法

java - 在 HttpRequestexecute() 上获取 NullPointerException 以将 POST 请求发送到 Google Places API

java spring命名查询和属性文件

java - NoClassDefFoundError 和 ClassNotFoundException

java - 适当释放I/O资源

java - 使用包含可编辑 JTextArea 的 JPanel 呈现 JTree 叶

java - 为 2 个管理员用户订阅 Quickbooks 中的应用

java - 在小程序查看器中调用时出现空白屏幕

java - 在 Reactor 中实现 while 循环以获取最新的 Elasticsearch 索引

java - 重用Spring MVC构建应用程序(Java)