Java 文件中的最后读取行打印为空

Java 文件中的最后读取行打印为空,java,file-io,io,Java,File Io,Io,我试图用我创建的方法读取整个文本文件。文本文件的所有行都会按照我的要求打印出来,但是文件的最后一行在打印出来时显示为空 private void readFile(String Path) throws IOException{ String text = ""; //String used in the process of reading a file //The file reader BufferedReader input = new BufferedRead

我试图用我创建的方法读取整个文本文件。文本文件的所有行都会按照我的要求打印出来,但是文件的最后一行在打印出来时显示为空

private void readFile(String Path) throws IOException{
    String text = ""; //String used in the process of reading a file

    //The file reader
    BufferedReader input = new BufferedReader(
            new FileReader(Path));

    //Creating a new string builder.
    StringBuilder stringBuilder = new StringBuilder();

    while(text != null)
    {
        //Read the next line
        text = input.readLine();

        stringBuilder.append(text); //Adds line of text into the String Builder
        stringBuilder.append(newLine); //Adds a new line using the newLine string
    } 

    //Sets the text that was created with the stringBuilder
    SetText(stringBuilder.toString());
}
所有文件都会100%打印出来,除了该方法在底部添加了一行额外的内容,上面写着“null”,我该如何编写代码,这样就不会出现这一行?

您可以更改以下内容:

    while(text != null)
    {
        //Read the next line
        text = input.readLine();

        // ... do stuff with text, which might be null now
    }
为此:

    while((text = input.readLine()) != null)
    {
        // ... do stuff with text
    }
或者这个:

    while(true)
    {
        //Read the next line
        text = input.readLine();
        if(text == null)
            break;

        // ... do stuff with text
    }
    text = input.readLine();
    while(text != null)
    {
        // ... do stuff with text

        //Read the next line
        text = input.readLine();
    }
或者这个:

    while(true)
    {
        //Read the next line
        text = input.readLine();
        if(text == null)
            break;

        // ... do stuff with text
    }
    text = input.readLine();
    while(text != null)
    {
        // ... do stuff with text

        //Read the next line
        text = input.readLine();
    }
根据您的喜好。

您可以更改此选项:

    while(text != null)
    {
        //Read the next line
        text = input.readLine();

        // ... do stuff with text, which might be null now
    }
为此:

    while((text = input.readLine()) != null)
    {
        // ... do stuff with text
    }
或者这个:

    while(true)
    {
        //Read the next line
        text = input.readLine();
        if(text == null)
            break;

        // ... do stuff with text
    }
    text = input.readLine();
    while(text != null)
    {
        // ... do stuff with text

        //Read the next line
        text = input.readLine();
    }
或者这个:

    while(true)
    {
        //Read the next line
        text = input.readLine();
        if(text == null)
            break;

        // ... do stuff with text
    }
    text = input.readLine();
    while(text != null)
    {
        // ... do stuff with text

        //Read the next line
        text = input.readLine();
    }

如您所愿。

您的循环退出条件位于错误的位置

while ((text = input.readLine()) != null) {
    stringBuilder.appendText(text)
    ...

循环退出条件位于错误的位置

while ((text = input.readLine()) != null) {
    stringBuilder.appendText(text)
    ...

使用预读,您将获得更干净的解决方案,这很容易理解:

text = input.readLine();

while(text != null)
    {
        stringBuilder.append(text); //Adds line of text into the String Builder
        stringBuilder.append(newLine); //Adds a new line using the newLine string

        //Read the next line
        text = input.readLine();
    } 

使用预读的原理,您几乎总能避免糟糕的退出条件。

使用预读,您将得到更清洁的解决方案,这很容易理解:

text = input.readLine();

while(text != null)
    {
        stringBuilder.append(text); //Adds line of text into the String Builder
        stringBuilder.append(newLine); //Adds a new line using the newLine string

        //Read the next line
        text = input.readLine();
    } 

使用预读原理,您几乎总能避免糟糕的退出条件。

或初始化text=input.readLine();循环前一次,循环体后一次。@jacobm:True。但有人真的喜欢这个吗?我其实有点喜欢,因为它最直接地对应于我用英语解释发生了什么。(虽然这需要你重复同一行两次。)@jacobm:很公平。添加::-)以下哪种方式可以让我的应用程序运行“最佳”或初始化text=input.readLine();循环前一次,循环体后一次。@jacobm:True。但有人真的喜欢这个吗?我其实有点喜欢,因为它最直接地对应于我用英语解释发生了什么。(虽然这需要你重复同一行两次。)@jacobm:很公平。添加::-)以下哪种方式是让我的应用程序运行“最佳”的方式?