在使用OPENCSV并尝试从一行读取特定列时,我遇到了一些小问题。我有一个csv文件,看起来像这样
"ID","Name","Name2","Date","Author"
"1","Alex","Example","18.3.2016","Alex"现在,我只想阅读第2和第3列(名称和Name2)。
我的代码如下所示
try {
CSVReader reader = new CSVReader(new FileReader(filelocation));
String [] nextLine;
int rowNumber = 0;
while ((nextLine = reader.readNext()) != null) {
rowNumber++;
for(int i = 0; i< nextLine.length ; i++){
System.out.println("Cell index: " + i);
System.out.println("Cell Value: " + nextLine[i]);
System.out.println("---");
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}我已经尝试手动将"i“变量设置为1和2,但是我得到了日志中显示的4个相同的结果。少了什么?谢谢!
发布于 2016-04-18 16:29:04
算了,我找到了诀窍。
看上去应该是这样的。
try {
CSVReader reader = new CSVReader(new FileReader(filelocation));
String [] nextLine;
int rowNumber = 0;
while ((nextLine = reader.readNext()) != null) {
rowNumber++;
String name = nextLine[1];
String name2 = nextLine[2];
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}之后,我可以得到字符串值并使用它。
https://stackoverflow.com/questions/36696960
复制相似问题