英文:
How to escape linebreak character while writing a csv with java/opencsv
问题
我有一个保存为Bean的CSV文件。其中一列包含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)
.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);
}
在ArrayList中的显示效果如下:
如何将其写入字符串?另外,如何将其导出为UTF-8编码的文件?
英文:
I have a CSV file, which is saved as a Bean. One column has HTML tags including \n character. While reading with opencsv is not a problem, but when i write a new csv it actually creates a new line break/row.
Here is what i have tried:
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);
}
and here is how it looks like in the arraylist:
How can i write it as a string? Also how can i export it as a UTF-8 encoded file?
答案1
得分: 0
@g00se的建议让我移除了.withEscapechar('\n')
,之后代码就正常工作了。我的最终导出代码如下:
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 "导出成功";
} catch (CsvRequiredFieldEmptyException | IOException e) {
throw new RuntimeException(e);
} catch (CsvDataTypeMismatchException e) {
throw new RuntimeException(e);
}
英文:
The suggestion of @g00se made me remove the .withEscapechar('\n') and after that it worked fine. My final Export looks like this:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论