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

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

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

问题

我有一个CSV文件,它被保存为一个Bean。其中一列包含HTML标签,包括\n字符。使用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") // 仅用于测试
            .withSeparator(',')
            .build();

    beanToCsv.write(data);
    writer.close();

} catch (CsvRequiredFieldEmptyException | IOException e) {
    throw new RuntimeException(e);
} catch (CsvDataTypeMismatchException e) {
    throw a 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&lt;Shopify&gt; 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-2.html
匿名

发表评论

匿名网友

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

确定