gpt4 book ai didi

java - 如何在java中从第二行读取CSV?

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:00 24 4
gpt4 key购买 nike

我有一个逗号分隔的 CSV 文件,并且我有从第二行读取它的代码。但它给了我以下错误。线程“AWT-EventQueue-0”中的异常 java.lang.OutOfMemoryError:Java 堆空间

这是我的代码。

public static List<String> readCSV(String path){        
BufferedReader br = null;
String[] cd = null;
String line ="";
String split=",";
List<String> list = new ArrayList<String>();
try {
br=new BufferedReader(new FileReader(path));
while ((line = br.readLine()) != null) {
boolean firstLine = true;
while (line != null && line.startsWith("child's Last Name")) {
if (firstLine) {
firstLine = false;
continue;
} else {
list.add(line);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return list;
}

如何使用此代码修复它..

提前致谢!

最佳答案

在第二个 while 循环中,您在哪里再次分配 line

 while (line != null && line.startsWith("child's Last Name"))

一旦满足此条件,您就不会重新分配它。
您的代码应该有所不同,例如,

boolean firstLine = true;               
while ((line = br.readLine()) != null)
{
if (firstLine)
{
firstLine = false;
continue;
}
else if(line != null && line.startsWith("child's Last Name"))
{
list.add(line);
}
}

关于java - 如何在java中从第二行读取CSV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20212089/

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