为什么文件中的行不是用java打印的

Muk*_*t09 -2 java file

我正在尝试用java读取文件.在该文件中,给出了一些我要打印的字符串.但我的代码只打印偶数行和跳过奇数行.

我在stackoverflow中搜索了它,但是之前没有找到解决方案.

我的代码如下:

//main class
import java.io.IOException;


public class takingInputFrpmFile {

    public static void main(String[] args) throws IOException {

        String filePath = "F:/Path/in.txt";

        try 
        {
            readFile rF = new readFile(filePath);
            String[] receivedArray = rF.Read();

            for(int i=0;i<receivedArray.length;i++)
                System.out.println(receivedArray[i]);

        }
        catch(IOException e)
        {
            System.out.println(e.getMessage());
        }
    }

}

// class called from main class

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;


public class readFile {

    private String path;

    public readFile(String path)
    {
        this.path=path;
    }

    public String[] Read() throws IOException 
    {
        FileReader fR = new FileReader(path);
        BufferedReader bR = new BufferedReader(fR);

        String[] textData = new String[110];
        String check;
        int i=0;

        while((check = bR.readLine()) != null)
        {
            textData[i] = bR.readLine();
            i++;
        }

        bR.close();
        return textData;
    }
}
Run Code Online (Sandbox Code Playgroud)

该文件包含这些行...

在此输入图像描述

这是我的代码的输出....

在此输入图像描述

我的代码出了什么问题?我应该改变什么?如何摆脱最后空值的打印?请帮助...提前致谢...

FIN*_*ide 5

您是第一次阅读该行并检查它是否为空,然后您读取另一行.

while((check = bR.readLine()) != null)
{
    textData[i] = check; //Changed this to check
    i++;
}
Run Code Online (Sandbox Code Playgroud)

那个会奏效.

您目前正在声明大小为110的String数组.您的文件真的是110行吗?您可能应该使用列表.

public List<String> Read() throws IOException 
{
    FileReader fR = new FileReader(path);
    BufferedReader bR = new BufferedReader(fR);

    List<String> textData = new ArrayList<>();
    String check;

    while((check = bR.readLine()) != null)
    {
        textData.add(check);
    }

    bR.close();
    return textData;
}
Run Code Online (Sandbox Code Playgroud)

如果你真的想要返回字符串数组,你可以使用:

return textData.toArray(new String[textData.size()]);
Run Code Online (Sandbox Code Playgroud)