0

Requirements are:

  • Every value in records should be quoted.
  • Column names should separated by comma and space(, ).

For example

Name, Age, Location
"A","154","California"
"B","2","New York"

How do I create such SuperCSV Preference for writing?

Nilesh
  • 2,089
  • 3
  • 29
  • 53
  • "But do not quote column names" and "Column names should separated by comma and space(, )" seems to be incompatible – davidxxx Jul 29 '16 at 13:57
  • 1
    Do not quote column names means ```[X] "Name","Age","Location" ```, Column Names should be separated by comma and space means [✓] `Name, Age, Location`. Are they incompatible? – Nilesh Aug 01 '16 at 06:38

1 Answers1

2

Use following Custom Quote Mode:

public class AllExceptHeaderQuoteMode implements QuoteMode {

    @Override
    public boolean quotesRequired(String csvColumn, CsvContext context, CsvPreference preference) {
        if (1 == context.getLineNumber()) {
            /*
             * Case whene column names are written
             */
            return false;
        } else {
            return true;
        }
    }
}

Using Custom Quote Mode:

CsvPreference preference = new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE).useQuoteMode(new AllExceptHeaderQuoteMode()).build());
Nilesh
  • 2,089
  • 3
  • 29
  • 53