【问题标题】:Using BufferedReader to count rows in a multi-dimensional array使用 BufferedReader 计算多维数组中的行数
【发布时间】:2011-10-22 09:55:51
【问题描述】:

我正在使用 BufferedReader 读取 .csv 文件。我读取文件和提取数据没有问题。但是,我确实遇到的问题是我必须对数组声明进行硬编码。例如:

        String[][] numbers=new String[5258][16];

我使用的 .csv 文件有 5258 行和 16 列。不过,我希望能够做这样的事情:

        String[][] numbers=new String[rowsInFile][16];

换句话说,我希望变量“rowsInFile”等于文件中的行数(我不想计算列,因为我将通过这个程序运行的每个 .csv 文件都有 16列)。

这是我目前的代码:

        int row = 0;
        int col = 0;

        String fileInput = JOptionPane.showInputDialog(null, 
                "Please enter the path of the CSV file to read:");

        File file = new File(fileInput);

        BufferedReader bufRdr;
        bufRdr = new BufferedReader(new FileReader(file));
        String line = null;

        //get rows in the file
        int rowsInFile = 0;
        while(bufRdr.readLine() != null) {
            rowsInFile++;
            row++;
        }
        String[][] numbers=new String[rowsInFile][16];

        //read each line of text file
        row = 0;
        while((line = bufRdr.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line,",");
            col=0;

            while (st.hasMoreTokens()) {
                //get next token and store it in the array
                numbers[row][col] = st.nextToken();
                col++;
            }
            row++;
        }

但是,我得到一个空指针异常。关于我应该做什么的任何想法?

附:是的,这段代码被 try/catch 语句包围。

【问题讨论】:

  • 哪一行抛出了 NPE,您发布的代码中没有任何可疑之处。
  • 很抱歉听起来很新手,但实际上我什至不知道如何检查。如果我单击显示的错误,它只会弹出一个名为“为 java.lang.NullPointerException 创建断点”的新窗口。

标签: java file-io multidimensional-array bufferedreader


【解决方案1】:

问题是,一旦你通过BufferedReader 一次,你就不能再通过它了。换句话说,你必须使用一个新的BufferedReader

bufRdr = new BufferedReader(new FileReader(file));
row = 0;
while((line = bufRdr.readLine()) != null) {

或者,您可以使用动态数组结构,例如 ArrayList<String[]>LinkedList<String[]> 来存储行。

LinkedList<String[]> numbers = new LinkedList<String[]>();

while( (line = bufRdr.readLine()) != null ) {
    numbers.add(line.split(","));
}

那么你不用numbers[i][j],而是使用numbers.get(i)[j]

【讨论】:

  • 我尝试添加 bufRdr = new BufferedReader(new FileReader(file));在字符串[][] 之后数字=新字符串[rowsInFile][16];再次创建一个新的 BufferedReader,但我仍然得到一个空指针异常。
  • 另外,我对使用列表有点陌生。您介意解释一下我如何使用它们来存储行吗?
  • @Mike Gates:嗯,这很奇怪。任何人,我编辑了我的答案以包含一个使用 LinkedList 的示例。
  • 我接受了你的回答,但我似乎仍然无法从列表转换为字符串[][]。
  • String[][] arr = new String[numbers.size()][]; for(int i = 0; i &lt; arr.length; i++) arr[i] = numbers.get(i);
【解决方案2】:

使用动态列表代替数组。例如:

List<String[]> data = new ArrayList<String[]>();

同样使用String的split()方法将简化行的加载。

【讨论】:

【解决方案3】:

您的问题是 BufferedReaders 通过读取直到文件末尾来工作,然后它们被卡在那里。您的代码需要读取文件两次,但由于您已经达到 EOF,因此 BufferedReader 卡在返回 null 中。我倾向于通过将行填充到 ArrayList 中并使用 size() 方法来获取行数来解决这个问题。源代码如下所示:

    int rowsInFile=0;
    ArrayList<String> lines = new ArrayList<String>();
    String tmp = "";
    while(tmp=bugRdr.readLine())
    {
        lines.add(tmp);
    }

    rowsInFile = lines.size();
    String[][] numbers = new String[rowsInFile][16];

    int row = 0;
    for(String line : lines)
    {
        StringTokenizer st = new StringTokenizer(line,",");
        col=0;

        while (st.hasMoreTokens()) {
        //get next token and store it in the array
            numbers[row][col] = st.nextToken();
            col++;
        }
        row++;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2016-10-13
    • 1970-01-01
    相关资源
    最近更新 更多