所以我的CSV文件包含类似excel
的内容 speed period warning pair
1 26 1 1 1
2 26 1 1 1
3 26 1 1 1
4 26 1 1 1
在我的扫描仪文件中,我有
public class scanner {
public static void main(String[] args) throws IOException {
// open file input stream
BufferedReader reader = new BufferedReader(new FileReader(
"amis.csv"));
// read file line by line
String line = null;
Scanner scanner = null;
int index = 0;
List<amis> empList = new ArrayList<>();
while ((line = reader.readLine()) != null) {
amis emp = new amis();
scanner = new Scanner(line);
scanner.useDelimiter(",");
while (scanner.hasNext()) {
String data = scanner.next();
if (index == 0) {
emp.setId(data);
} else if (index == 4) {
emp.setPair(data);
} else if (index == 3) {
emp.setWarning(data);
} else if (index == 2) {
emp.setPeriod(data);
}else if (index == 1) {
emp.setSpeed(data);
}else {
System.out.println("invalid data::" + data);
}
index++;
}
index = 0;
empList.add(emp);
}
//close reader
reader.close();
System.out.println(empList);
}
但我的链表的输出会显示
ID =&#34;&#34; ::对=&#34;一对&#34; :: =警告&#34;警告&#34; ::周期=&#34;周期&#34 ;: :速度=&#34;速度&#34;,
ID =&#34; 1&#34; ::对= 1 ::警告= 1个::周期= 1 ::速度= 26,
ID =&#34; 2&#34; :: Pair = 1 :: Warning = 1 :: Period = 1 :: Speed = 26,
ID =&#34; 3&#34; :: Pair = 1 :: Warning = 1 :: Period = 1 :: Speed = 26,
ID =&#34; 4&#34; :: Pair = 1 :: Warning = 1 :: Period = 1 :: Speed = 26,
如何从第二行开始。
答案 0 :(得分:2)
您可以在while循环之前调用scanner.nextLine
,例如:
String headers = reader.nextLine();
while ((line = reader.readLine()) != null) {
这是文档所说的:
使此扫描程序超过当前行并返回该输入 被跳过了。此方法返回当前行的其余部分, 排除末尾的任何行分隔符。该职位设定为 下一行的开头。
因此,在while循环中,您将获得第二行的所有行。