如何在使用Java/opencsv编写CSV文件时避免换行字符。

huangapple go评论122阅读模式
英文:

How to escape linebreak character while writing a csv with java/opencsv

问题

我有一个CSV文件,它被保存为一个Bean。其中一列包含HTML标签,包括\n字符。使用opencsv读取时没有问题,但当我写一个新的CSV文件时,它实际上创建了一个新的换行/行。

这是我尝试过的方法:

  1. File file = new File(outDirectory, this.outputCSVName);
  2. try (
  3. Writer writer = Files.newBufferedWriter(Paths.get(file.toURI()));
  4. ) {
  5. StatefulBeanToCsv<Shopify> beanToCsv = new StatefulBeanToCsvBuilder(writer)
  6. .withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
  7. .withApplyQuotesToAll(true)
  8. .withEscapechar('\n')
  9. .withLineEnd("\n") // 仅用于测试
  10. .withSeparator(',')
  11. .build();
  12. beanToCsv.write(data);
  13. writer.close();
  14. } catch (CsvRequiredFieldEmptyException | IOException e) {
  15. throw new RuntimeException(e);
  16. } catch (CsvDataTypeMismatchException e) {
  17. throw a RuntimeException(e);
  18. }

以及在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. 如何在使用Java/opencsv编写CSV文件时避免换行字符。

Here is what i have tried:

  1. File file = new File(outDirectory, this.outputCSVName);
  2. try (
  3. Writer writer = Files.newBufferedWriter(Paths.get(file.toURI()));
  4. ) {
  5. StatefulBeanToCsv&lt;Shopify&gt; beanToCsv = new StatefulBeanToCsvBuilder(writer)
  6. .withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
  7. .withApplyQuotesToAll(true)[![enter image description here][1]][1]
  8. .withEscapechar(&#39;\n&#39;)
  9. .withLineEnd(&quot;\n&quot;) // just to test
  10. .withSeparator(&#39;,&#39;)
  11. .build();
  12. beanToCsv.write(data);
  13. writer.close();
  14. } catch (CsvRequiredFieldEmptyException | IOException e) {
  15. throw new RuntimeException(e);
  16. } catch (CsvDataTypeMismatchException e) {
  17. throw new RuntimeException(e);
  18. }

and here is how it looks like in the arraylist:如何在使用Java/opencsv编写CSV文件时避免换行字符。

How can i write it as a string? Also how can i export it as a UTF-8 encoded file?

答案1

得分: 0

  1. @g00se的建议让我移除了*.withEscapechar('\n')*之后它就正常工作了我的最终导出结果如下
  2. File file = new File(outDirectory, this.outputCSVName);
  3. try (
  4. Writer writer = Files.newBufferedWriter(Paths.get(file.toURI()), StandardCharsets.UTF_8);
  5. ) {
  6. StatefulBeanToCsv&lt;Shopify&gt; beanToCsv = new StatefulBeanToCsvBuilder(writer)
  7. .withSeparator(',')
  8. .build();
  9. beanToCsv.write(data);
  10. writer.close();
  11. return "导出成功";
  12. } catch (CsvRequiredFieldEmptyException | IOException e) {
  13. throw new RuntimeException(e);
  14. } catch (CsvDataTypeMismatchException e) {
  15. throw new RuntimeException(e);
  16. }
英文:

The suggestion of @g00se made me remove the .withEscapechar('\n') and after that it worked fine. My final Export looks like this:

  1. File file = new File(outDirectory, this.outputCSVName);
  2. try (
  3. Writer writer = Files.newBufferedWriter(Paths.get(file.toURI()), StandardCharsets.UTF_8);
  4. ) {
  5. StatefulBeanToCsv&lt;Shopify&gt; beanToCsv = new StatefulBeanToCsvBuilder(writer)
  6. .withSeparator(&#39;,&#39;)
  7. .build();
  8. beanToCsv.write(data);
  9. writer.close();
  10. return &quot;Export OK&quot;;
  11. } catch (CsvRequiredFieldEmptyException | IOException e) {
  12. throw new RuntimeException(e);
  13. } catch (CsvDataTypeMismatchException e) {
  14. throw new RuntimeException(e);
  15. }

huangapple
  • 本文由 发表于 2023年8月9日 16:40:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865987-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定