我想将上面的代码用于另一个参数,例如args[2]
。我应该在哪里进行更改? nextline
会保持不变还是会变成 nextline[1]
?
最佳答案
假设args[2]
保存第二个数据输入文件的名称,那么您必须为该文件创建第二个读取器。一个简单的 for 循环的快速解决方案,跳过异常处理:
for (int i = 1; i <= 2; i++){
CSVReader reader = new CSVReader(new FileReader(args[i]));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
//System.out.println(nextLine[0] + nextLine[1] + "etc...");
for (String value:nextLine)
System.out.print(value+" ");
System.out.println();
}
reader.close();
}
一行中值的数量可以通过 nextLine.length
确定.
关于Java:语法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4105103/