【发布时间】:2019-12-11 11:16:03
【问题描述】:
我使用 spring-csv 来读取 csv 文件,例如此处的示例
为了这个目标我创建了这个方法
public static <T> List<T> loadObjectList(Class<T> type, File file) {
try {
CsvSchema bootstrapSchema = CsvSchema.emptySchema().withHeader().withColumnSeparator('$');
CsvMapper mapper = new CsvMapper();
MappingIterator<T> readValues = mapper
.reader(type)
.with(bootstrapSchema).read
.readValues(file);
List<T> values = readValues.readAll();
return values;
} catch (Exception e) {
System.err.format("Error occurred while loading object list from file %s", file.getName(), e);
return Collections.emptyList();
}
}
它适用于具有 50-60k 记录行的文件。 但是,现在我需要读取包含 1100 万条记录的文件并在任何线程中处理它。 对于线程来说最好使用Queues,那么,如何将此方法转换为返回Queues而不是ArrayList?
【问题讨论】:
标签: java spring list csv queue