简体   繁体   中英

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

I am using CSVPrinter in order to read and write CSV. While writing CSV, I want to write a blank column such like that "x",,"y" (the second column). But unfortunately it writes as "x","","y". How I can write a blank column by using CSVPrinter? I used following stuff in order to write it . Even, I could not find it in the CSVPrinter documentation. Please help me.

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

Thanks

The behavior you are looking for was added only in version 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:

    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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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