首先,我是 Java 新手,我需要一些帮助来解决以下问题。
我有一个包含数百万条记录的 Java 列表。我想将此数据列表导出为 CSV 文件。以下是我为读取数据而编写的函数,但它对于数百万条记录都失败了。你能建议任何更好的方法吗?
private String generateCsvData(List<Map<String, Object>> rows) {
String output = reportService.getReportHeadder();
for (Map row : rows) {
output += (Long) row.get("branchId")
+ ","
+ StringEscapeUtils.escapeCsv((String) row.get("branches"))
+ ","
+ StringEscapeUtils.escapeCsv((String) row
.get("categoryName"))
+ ","
+ StringEscapeUtils.escapeCsv((String) row.get("products"))
+ ","
+ StringEscapeUtils.escapeCsv((String) row.get("emails"))
+ ","
+ StringEscapeUtils.escapeCsv((String) row
.get("contactAddress"))
+ ","
+ StringEscapeUtils.escapeCsv((String) row
.get("contactDet")) + ","
+ StringEscapeUtils.escapeCsv((String) row.get("url"))
+ "\n";
}
return output;
}