如何在使用Java/opencsv写入CSV文件时转义换行符?

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

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. 如何在使用Java/opencsv写入CSV文件时转义换行符?

Here is what i have tried:

File file = new File(outDirectory, this.outputCSVName);

    try (
            Writer writer = Files.newBufferedWriter(Paths.get(file.toURI()));
    ) {
        StatefulBeanToCsv&lt;Shopify&gt; beanToCsv = new StatefulBeanToCsvBuilder(writer)
                .withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
                .withApplyQuotesToAll(true)[![enter image description here][1]][1]
                .withEscapechar(&#39;\n&#39;)
                .withLineEnd(&quot;\n&quot;) // just to test
                .withSeparator(&#39;,&#39;)
                .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:如何在使用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

@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&lt;Shopify&gt; beanToCsv = new StatefulBeanToCsvBuilder(writer)
                .withSeparator(&#39;,&#39;)
                .build();

        beanToCsv.write(data);
        writer.close();
        return &quot;Export OK&quot;;
    } catch (CsvRequiredFieldEmptyException | IOException e) {
        throw new RuntimeException(e);
    } catch (CsvDataTypeMismatchException e) {
        throw new RuntimeException(e);
    }

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

发表评论

匿名网友

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

确定