简体   繁体   English

如何使用apache.commons.csv的CSVPrinter写空白列

[英]how to write a blank column with CSVPrinter of apache.commons.csv

I am using CSVPrinter in order to read and write CSV. 我正在使用CSVPrinter来读写CSV。 While writing CSV, I want to write a blank column such like that "x",,"y" (the second column). 在编写CSV时,我想写一个空白列,例如“ x”,“ y”(第二列)。 But unfortunately it writes as "x","","y". 但不幸的是,它写为“ x”,“”,“ y”。 How I can write a blank column by using CSVPrinter? 如何使用CSVPrinter写空白列? I used following stuff in order to write it . 我用下面的东西写它。 Even, I could not find it in the CSVPrinter documentation. 甚至,我在CSVPrinter文档中也找不到它。 Please help me. 请帮我。

printer.print(null) -> "" printer.print("") -> "" printer.print(null)->“” printer.print(“”)->“”

Thanks 谢谢

The behavior you are looking for was added only in version 1.5: 您要查找的行为仅在1.5版中添加:

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-csv</artifactId>
        <version>1.5</version>
    </dependency>

With this API you can apply ALL_NON_NULL quote mode: 使用此API,您可以应用ALL_NON_NULL引用模式:

    CSVFormat customFormat = CSVFormat.DEFAULT
            .withQuoteMode(QuoteMode.ALL_NON_NULL);

    CSVPrinter printer = new CSVPrinter(System.out, customFormat);
    printer.print("x");
    printer.print("");
    printer.print(null);
    printer.print("y");
    printer.println();

Output: 输出:

"x","",,"y"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM