我有一个 CSV 文件,需要在扫描仪的帮助下逐行读取,并仅将国家/地区名称存储到字符串数组中。这是我的 CSV 文件:
World Development Indicators
Number of countries,4
Country Name,2005,2006,2007
Bangladesh,6.28776238,13.20573922,23.46762823
"Bahamas,The",69.21279415,75.37855087,109.340767
Brazil,46.31418452,53.11025849,63.67475185
Germany,94.55486999,102.2828888,115.1403608
这是我到目前为止所拥有的:
public String[] getCountryNames() throws IOException, FileNotFoundException{
String[] countryNames = new String[3];
int index = 0;
BufferedReader br = new BufferedReader(new FileReader(fileName));
br.readLine();
br.readLine();
br.readLine();
String line = br.readLine();
while((br.readLine() != null) && !line.isEmpty()){
String[] countries = line.split(",");
countryNames[index] = countries[0];
index++;
line = br.readLine();
}
System.out.println(Arrays.toString(countryNames));
return countryNames;
}
输出:
[Bangladesh, Brazil, null]
出于某种原因,它会跳过“Bahamas, The”并且无法读取德国。请帮助我,我已经坚持这个方法几个小时了。感谢您的时间和精力。返回应该是一个字符串数组(国家/地区名称)。
最佳答案
用于解析此 CSV 文件的代码存在两个问题。正如一些人指出的那样,您正在调用 readLine
在你的读者上太多次,并丢弃输出。每次从流中读取数据时,您都将无法访问当前读取点之前的任何数据。例如,reader.readLine() != null 会从流中读取新数据,检查它是否为空,然后立即删除它,因为您尚未将其存储在一个变量。这是您在阅读时丢失数据的主要原因。
第二个问题是你的分割条件。您以逗号分隔,这是有道理的,因为这是一个 CSV 文件,但您的数据也包含逗号(例如,“Bahamas, The”)。您将需要更具体的分割条件,如 this post 中所述。 .
下面是一个示例(使用 countryNames
列表而不是数组,因为这样更容易使用):
private static final String csv = "World Development Indicators\n"
+ "Number of countries,4\n"
+ "Country Name,2005,2006,2007\n"
+ "Bangladesh,6.28776238,13.20573922,23.46762823\n"
+ "\"Bahamas,The\",69.21279415,75.37855087,109.340767\n"
+ "Brazil,46.31418452,53.11025849,63.67475185\n"
+ "Germany,94.55486999,102.2828888,115.1403608\n";
public static String[] getCountryNames() throws Exception {
List<String> countryNames = new ArrayList<>();
//BufferedReader br = new BufferedReader(new FileReader(fileName));
BufferedReader br = new BufferedReader(new StringReader(csv));
br.readLine();
br.readLine();
br.readLine();
String line = br.readLine();
while (line != null && !line.isEmpty()) {
String[] countries = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
countryNames.add(countries[0]);
line = br.readLine();
}
System.out.println(countryNames);
return countryNames.toArray(new String[0]);
}
关于java - 逐行读取CSV文件并解析它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48492889/