gpt4 book ai didi

Java - CSV 中的换行符转义

转载 作者:行者123 更新时间:2023-12-02 01:10:59 24 4
gpt4 key购买 nike

我正在尝试使用以下代码将数据从数据库导出到 CSV 文件。我可以使用下面的代码生成下面的结果,但这不是我想要的结果

Wrong Result: 
1. "abcd","123","123","test","abcd","123"
2. "abcd12","222", "333", "testing", "abc", "ccc"
3. "abc123", "22 :
sdds", "sds", "s123", "54"

前两行结果是正确的。但是,在第三行中,当特殊字符 : 出现在第 3 行的第二个单元格中时,就会发生换行。我正在寻找的结果是:是否有任何建议可以使用以下代码生成以下结果?

Expected Result
1. "abcd","123","123","test","abcd","123"
2. "abcd12","222", "333", "testing", "abc", "ccc"
3. "abc123", "22 : sdds", "sds", "s123", "54"

代码:

try (Writer out = new PrintWriter(path, "UTF-8");
CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT.RFC4180.withHeader(resultSet).withQuoteMode(QuoteMode.ALL))) {


while(resultSet.next()) {
final List<String> columnList = new LinkedList<String>();
rowList.add(columnList);

for (int column = 1; column <= columnCount; ++column)
{

printer.print(resultSet.getString(column));

}

printer.println();

}
printer.flush();
printer.close();


}

最佳答案

替换

printer.print(resultSet.getString(column));

printer.print(resultSet.getString(column).replace(":", "\":"));

关于Java - CSV 中的换行符转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59414661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com