Search code examples
csvsupercsv

How to always apply quotes (escape) in Super CSV


i would like to put my file csv like this forma:

"User 2";"Web Page" ;"AbdelMonem NAAMANE";;;;

i make my own Csv Preference like that:

private static final CsvPreference myPreference = new CsvPreference.Builder('"',';''\n').build();

i would like to have a header like that "User 2";"Web Page"


Solution

  • It's hard to see what you're doing without a complete code example, but from your post on the Super CSV bug tracker, I suspect that you're asking how to always quote CSV fields.

    There's extensive documentation on the Super CSV website on how to configure custom preferences - the feature you're after is the quote mode.

    Here's an example of using the AlwaysQuote quote mode, which quotes every field even if it doesn't contain special characters.

    CsvPreference prefs = new CsvPreference.Builder('"',';',"\n")
        .useQuoteMode(new AlwaysQuoteMode()).build();
    
    ICsvListWriter writer = new CsvListWriter(new OutputStreamWriter(System.out), 
        prefs);
    writer.writeHeader("User2", "Web Page");
    writer.flush();
    

    Which prints:

    "User2";"Web Page"