0

我正在使用 jackson-dataformat-csv lib 将对象列表写入 CSV 文件。但是当我打开它时,最后一个空行。写作时如何删除这一行?下面是我的代码。

public static void writeAccountToFile(List<Map<String, Object>> list, String filePath) {
    System.out.println("Write data to csv file start");
    try {
        File file = new File(filePath);
        Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        CsvSchema schema = null;
        CsvSchema.Builder schemaBuilder = CsvSchema.builder();
        if (list != null && !list.isEmpty()) {
            for (String col : list.get(0).keySet()) {
                schemaBuilder.addColumn(col);
            }   
        } else {
              schemaBuilder.addColumn("test1");
              schemaBuilder.addColumn("test2");
        }
        schema = schemaBuilder.build().withLineSeparator("\r").withHeader();
        CsvMapper mapper = new CsvMapper();
        mapper.configure(CsvGenerator.Feature.STRICT_CHECK_FOR_QUOTING, true);
        mapper.writer(schema).writeValues(writer).writeAll(list);
        writer.flush();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

感谢您的阅读。

4

0 回答 0