我想从 CSV 读取大量数据,包含大约 500,000 行。 我正在使用 OpenCSV 库。我的代码是这样的
CsvToBean<User> csvConvertor = new CsvToBean<User>();
List<User> list = null;
try {
list =csvConvertor.parse(strategy, new BufferedReader(new FileReader(filepath)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
最多 200,000 条记录,数据被读入 User bean 对象列表中。但对于比我得到的更多的数据
java.lang.OutOfMemoryError: Java heap space
我在“eclipse.ini”文件中有这个内存设置
-Xms256m
-Xmx1024m
我正在考虑将大文件拆分为单独文件并再次读取这些文件的解决方案,我认为这是一个冗长的解决方案。
有没有其他方法可以避免 OutOfMemoryError 异常。
最佳答案
逐行阅读
类似这样的事情
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
关于java - 在java中读取大型CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60918668/