gpt4 book ai didi

java - 在java中读取csv时如何跳过第一行

转载 作者:行者123 更新时间:2023-12-01 13:36:27 25 4
gpt4 key购买 nike

我想跳过第一行的第一列并将每个元素与字符串进行比较。 Java代码

 String csvFileToRead = "C:\\Users\\Dell\\Downloads\\Tasks.csv";        
BufferedReader br = null;
String[] Task = null;
String line;
String splitBy = ",";
br = new BufferedReader(new FileReader(csvFileToRead));
System.out.println("Excel data for Subejct: ");
while ((line = br.readLine()) != null) {
Task = line.split(splitBy);
System.out.println(Task[0]);
}

最佳答案

要跳过第一行,只需执行br.readLine()。要跳过第一列,请开始迭代 1 处的单元格,而不是 0

请参阅此示例:

br.readLine(); //skips the first row
while ((line = br.readLine()) != null) {
String[] cells = line.split(splitBy);
//starting from 1 essentially skips the first column
for (int col = 1; col < cells.length; col++) {
if (cells[col].equals(someString)) {
//do something
}
}
}

关于java - 在java中读取csv时如何跳过第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21231125/

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