3

当 CSVFormat 的 quoteMode 设置为 MINIMAL 时,根据javadoc ,仅当值包含分隔符 (,) 或行分隔符 '\n' 时才应引用该值。但即使值包含哈希 (#),它也会引用。请看下面的示例代码

  @Test
  public void testCSV() throws IOException {
    String[] row = new String[]{"simple text","##5##"};
    CSVFormat CSV_FORMAT = CSVFormat.DEFAULT.withRecordSeparator("\n").withQuoteMode(QuoteMode.MINIMAL);

    StringWriter stringWriter = new StringWriter(1024);
    CSVPrinter csvPrinter = new CSVPrinter(stringWriter, CSV_FORMAT);
    csvPrinter.printRecord(row);
    System.out.println("sw : " + stringWriter.getBuffer().toString());
    csvPrinter.close();
    stringWriter.close();
  }

输出:sw:简单文本,“##5##”

请提出一种我可以避免在这种情况下引用的方法。

4

0 回答 0