简体   繁体   English

如何检测文件中的新行(或空行)?

[英]How to detect new line(or empty line) in a file?

I'm trying to write a program, which reads text from a file that is specified by the user. 我正在尝试编写一个程序,该程序从用户指定的文件中读取文本。 Now, this program should detect an empty line. 现在,该程序应该检测到一个空行。

This is what I have unsuccessfully tried: 这是我尝试失败的内容:

public static void editFile(String filePath) throws FileNotFoundException, IOException {
    file = new File(filePath);
    if(file.exists()) {
        fileRead = new FileReader(file);

        bufferedReader = new BufferedReader(fileRead);

        String line = bufferedReader.readLine();
        System.out.println(line);
        while(line != null) {
            line = bufferedReader.readLine();
            if(line == "") {
                //line = null;
                System.out.println("a");
            }
            System.out.println(line);
        }
    }
}

To be more clear: 更清楚地说:

If I'm passing in a text file with for example this text: 如果我传入的文本文件包含以下文本:

test1 测试1
test2 测试2

test3 测试3

test4 测试4

it should print 2 a's in the console because of the empty spaces, but it doesn't. 由于空格,它应该在控制台上打印2 a,但不是。

Thank you for your time, I am glad for any suggestion you may have. 谢谢您的宝贵时间,我很高兴收到您的任何建议。

This is because the comparison is wrong. 这是因为比较是错误的。 You can't use == to compare two strings, you need to use the equals method: 您不能使用==比较两个字符串,需要使用equals方法:

if(line.equals(""))

Since you are checking for empty string, you can also write 由于您要检查的是空字符串,因此您还可以编写

if(line.isEmpty())

How do I compare strings in java? 如何比较Java中的字符串?

BackSlash is entirely correct, and has answered your question. BackSlash是完全正确的,并且已经回答了您的问题。 I'd like to add that your code has some errors: 我想补充一点,您的代码有一些错误:

  • You're not closing the Reader 您没有关闭阅读器
  • You're not testing the first line for blank 您没有测试第一行的空白
  • You're processing the null value when reaching EOF 达到EOF时,您正在处理null

The following corrects these errors. 以下内容纠正了这些错误。

public static void editFile(String filePath) throws IOException
{
    File file = new File(filePath);
    if (file.exists())
    {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        try
        {
            String line;
            while ((line = bufferedReader.readLine()) != null)
            {
                if (line.isEmpty())
                {
                    //line = null;
                    System.out.println("a");
                }
                System.out.println(line);
            }
        } finally {
            bufferedReader.close();
        }
    }
}

Output is: 输出为:

test1
test2
a

test3
a

test4

Note: You're still printing the blank line in addition to the "a". 注意:除了“ a”之外,您还在打印空白行。

What you're doing wrong is that you're comparing the variable itself, not its value with a null string. 您做错的是您正在比较变量本身,而不是将其值与空字符串进行比较。 Generally there are built-in functions in the string class that return true & false for checking if it's == with something. 通常, 字符串类中具有内置的函数,这些函数返回truefalse来检查其是否等于==。

if(line.equals("")) { ... }

Or you can just use any alternative way. 或者,您也可以使用任何其他方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM