我有一个 CSV 文件,它保存为 Bean。一列包含 HTML 标签,包括 特点。虽然使用 opencsv 读取不是问题,但当我编写新的 csv 时,它实际上会创建一个新的换行符/行。
这是我尝试过的:
File file = new File(outDirectory, this.outputCSVName);
try (
Writer writer = Files.newBufferedWriter(Paths.get(file.toURI()));
) {
StatefulBeanToCsv<Shopify> beanToCsv = new StatefulBeanToCsvBuilder(writer)
.withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
.withApplyQuotesToAll(true)[![enter image description here][1]][1]
.withEscapechar('\n')
.withLineEnd("\n") // just to test
.withSeparator(',')
.build();
beanToCsv.write(data);
writer.close();
} catch (CsvRequiredFieldEmptyException | IOException e) {
throw new RuntimeException(e);
} catch (CsvDataTypeMismatchException e) {
throw new RuntimeException(e);
}
如何将其写为字符串?另外我如何将其导出为 UTF-8 编码文件?
@g00se 的建议让我删除了 .withEscapechar(' ') 之后效果很好。我的最终导出如下所示:
File file = new File(outDirectory, this.outputCSVName);
try (
Writer writer = Files.newBufferedWriter(Paths.get(file.toURI()), StandardCharsets.UTF_8);
) {
StatefulBeanToCsv<Shopify> beanToCsv = new StatefulBeanToCsvBuilder(writer)
.withSeparator(',')
.build();
beanToCsv.write(data);
writer.close();
return "Export OK";
} catch (CsvRequiredFieldEmptyException | IOException e) {
throw new RuntimeException(e);
} catch (CsvDataTypeMismatchException e) {
throw new RuntimeException(e);
}