有没有人有使用Java中的opencsv编写仅需将某些元素加双引号的csv文件的经验?我想要测试的期望输出是制作一个文件,内容为:
1,"two",three
但是当我尝试以下代码时

writer = new CSVWriter(new FileWriter("yourfile.csv"), ',',CSVWriter.NO_QUOTE_CHARACTER);
String[] entries = {"1","\"two\"","three"};
writer.writeNext(entries);
writer.close();

出现以下输出
1,""two"",three
有什么想法吗?

最佳答案

这些额外的引号是OpenCSV使用的转义字符。您需要使用overloaded constructor来关闭这些功能:

writer = new CSVWriter(new FileWriter("yourfile.csv"), ',',
           CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER);
...

输出:
1,"two",three

10-01 22:58