巨大文件的奇怪BufferedReader行为

时间:2022-06-01 13:32:44

I am getting a very weird error. So, my program read a csv file.

我收到一个非常奇怪的错误。所以,我的程序读取了一个csv文件。

Whenever it comes to this line:

每当涉及到这条线:

"275081";"cernusco astreet, milan, italy";NULL

I get an error:

我收到一个错误:

巨大文件的奇怪BufferedReader行为

In the debug screen, I see that the BufferedReader read only

在调试屏幕中,我看到BufferedReader只读

"275081";"cernusco as

That is a part of the line. But, it should read all of the line. 巨大文件的奇怪BufferedReader行为

这是该行的一部分。但是,它应该阅读所有的内容。

What bugs me the most is when I simply remove that line out of the csv file, the bug disappear! The program runs without any problem. I can remove the line, maybe it is a bad input or whatever; but, I want to understand why I am having this problem.

最让我烦恼的是当我从csv文件中删除该行时,该bug就消失了!该程序运行没有任何问题。我可以删除该行,也许这是一个糟糕的输入或其他;但是,我想知道为什么我遇到这个问题。

For better understanding, I will include a part of my code here:

为了更好地理解,我将在此处包含我的部分代码:

        reader = new BufferedReader(new FileReader(userFile));
        reader.readLine(); // skip first line
        while ((line = reader.readLine()) != null) {
            String[] fields = line.split("\";\"");
            int id = Integer.parseInt(stripPunctionMark(fields[0]));
            String location = fields[1];
            if (location.contains("\";")) { // When there is no age. The data is represented as "location";NULL. We cannot split for ";" here. So check for "; and split.
                location = location.split("\";")[0];
                System.out.printf("Added %d at %s\n", id, location);
                people.put(id, new Person(id, location));
                numberOfPeople++;
            }
            else {
                int age = Integer.parseInt(stripPunctionMark(fields[2]));
                people.put(id, new Person(id, location, age));
                System.out.printf("Added %d at: %s age: %d \n", id, location, age);
                numberOfPeople++;
            }

Also, you can find the csv file here or here is a short version of the part that I encountered the error:

另外,你可以在这里找到csv文件,或者这里是我遇到错误的部分的简短版本:

"275078";"el paso, texas, usa";"62"
"275079";"istanbul, eurasia, turkey";"26"
"275080";"madrid, n/a, spain";"29"
"275081";"cernusco astreet, milan, italy";NULL
"275082";"hacienda heights, california, usa";"16"
"275083";"cedar rapids, iowa, usa";"22"

1 个解决方案

#1


3  

This has nothing whatsoever to do with BufferedReader. It doesn't even appear in the stack trace.

这与BufferedReader没有任何关系。它甚至没有出现在堆栈跟踪中。

It has to do with your failure to check the result and length of the array returned by String.split(). Instead you are just assuming the input is well-formed, with at least three columns in each row, and you have no defences if it isn't.

它与您未能检查String.split()返回的数组的结果和长度有关。相反,您只是假设输入格式正确,每行至少有三列,如果不是,则没有防御。

#1


3  

This has nothing whatsoever to do with BufferedReader. It doesn't even appear in the stack trace.

这与BufferedReader没有任何关系。它甚至没有出现在堆栈跟踪中。

It has to do with your failure to check the result and length of the array returned by String.split(). Instead you are just assuming the input is well-formed, with at least three columns in each row, and you have no defences if it isn't.

它与您未能检查String.split()返回的数组的结果和长度有关。相反,您只是假设输入格式正确,每行至少有三列,如果不是,则没有防御。