Java 大文件的奇怪BufferedReader行为

Java 大文件的奇怪BufferedReader行为,java,string,csv,split,Java,String,Csv,Split,我犯了一个很奇怪的错误。所以,我的程序读取一个csv文件 每当涉及到这一行时: "275081";"cernusco astreet, milan, italy";NULL 我得到一个错误: 在调试屏幕中,我看到BufferedReader是只读的 "275081";"cernusco as 这是路线的一部分。但是,它应该读取所有行。 最让我头疼的是,当我简单地从csv文件中删除该行时,这个bug就消失了!程序运行没有任何问题。我可以删除该行,可能是输入错误或其他原因;但是,我想知道为什

我犯了一个很奇怪的错误。所以,我的程序读取一个csv文件

每当涉及到这一行时:

"275081";"cernusco astreet, milan, italy";NULL
我得到一个错误:

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

"275081";"cernusco as
这是路线的一部分。但是,它应该读取所有行。

最让我头疼的是,当我简单地从csv文件中删除该行时,这个bug就消失了!程序运行没有任何问题。我可以删除该行,可能是输入错误或其他原因;但是,我想知道为什么我会有这个问题

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

        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++;
            }
此外,您可以找到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"

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


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

您是否尝试过用有效字符串替换“NULL”?我刚刚将行更改为:“275081”;“cernusco as treet,米兰,意大利”;但问题仍然存在。在这行之前有很多空值。而且,我的程序处理得很好。就是这一行让我抓狂。似乎在
as
后面有一个隐藏字符。你能提取文件的那一部分并用十六进制转储文件查看吗?用十六进制编辑器查看文件-那一行有没有奇怪的字符?哪一行是44?文件底部是否有空行?是否有Java库为我们完成所有这些工作?例如,只要输入一个文件,它就会给出一个映射或数组。