大文件的奇怪 BufferedReader 行为
Weird BufferedReader behavior for a huge file
我收到一个非常奇怪的错误。所以,我的程序读取了一个 csv 文件。
每当谈到这一行时:
"275081";"cernusco astreet, milan, italy";NULL
我得到一个错误:
在调试屏幕中,我看到 BufferedReader 是只读的
"275081";"cernusco as
这是该行的一部分。但是,它应该读取所有行。
最让我烦恼的是,当我简单地从 csv 文件中删除该行时,错误就消失了!程序运行没有任何问题。我可以删除该行,也许这是一个错误的输入或其他什么;但是,我想了解为什么我会遇到这个问题。
为了更好地理解,我将在此处包含我的部分代码:
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 文件 here 或者这里是我遇到错误的部分的简短版本:
"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().
返回的数组的结果和长度有关,相反,您只是假设输入格式正确,每行至少有三列,并且如果不是,你就没有防御能力。
我收到一个非常奇怪的错误。所以,我的程序读取了一个 csv 文件。
每当谈到这一行时:
"275081";"cernusco astreet, milan, italy";NULL
我得到一个错误:
在调试屏幕中,我看到 BufferedReader 是只读的
"275081";"cernusco as
这是该行的一部分。但是,它应该读取所有行。
最让我烦恼的是,当我简单地从 csv 文件中删除该行时,错误就消失了!程序运行没有任何问题。我可以删除该行,也许这是一个错误的输入或其他什么;但是,我想了解为什么我会遇到这个问题。
为了更好地理解,我将在此处包含我的部分代码:
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 文件 here 或者这里是我遇到错误的部分的简短版本:
"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().
返回的数组的结果和长度有关,相反,您只是假设输入格式正确,每行至少有三列,并且如果不是,你就没有防御能力。