gpt4 book ai didi

java - 如何从 CSV 数据的第二行开始扫描到链接列表?

转载 作者:行者123 更新时间:2023-11-30 02:38:56 25 4
gpt4 key购买 nike

所以我的 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=""::Pair="pair"::Warning="警告"::Period="period"::Speed="speed",

ID="1"::Pair=1::Warning=1::Period=1::Speed=26,

ID="2"::Pair=1::Warning=1::Period=1::Speed=26,

ID="3"::Pair=1::Warning=1::Period=1::Speed=26,

ID="4"::Pair=1::Warning=1::Period=1::Speed=26,

如何从第二行开始。

最佳答案

您可以在 while 循环之前调用 scanner.nextLine,例如:

String headers = reader.nextLine();
while ((line = reader.readLine()) != null) {

文档是这样说的:

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

因此,在 while 循环中,您将获得第二行中的所有行。

关于java - 如何从 CSV 数据的第二行开始扫描到链接列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42319341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com