Unwanted double quotes in generated csv file

This worked for me

CSVWriter writer = 
    new CSVWriter(new FileWriter(csv), ',', CSVWriter.NO_QUOTE_CHARACTER);

See the CSVWriter javadoc


You should probably clarify what you mean by 'unwanted' quotes.

  1. I don't want it to quote everything, only the fields that contain embedded commas, quotes and newlines (quoting everything is unnecessary and makes my files bigger), or

  2. I don't want anything quoted, and I understand that my CSV will be invalid if it contains embedded commas, quotes and newlines

If it's the first option, then opencsv doesn't support this - it either quotes everything or nothing. Take a look at Super CSV if you want an open source CSV library that only quotes when necessary (and can quote everything too, if required).

If it's the second option, then go with Sheldon's answer, but just be aware the your CSV will be invalid if it contains embedded commas, quotes and newlines.

For example, if I'm reading your CSV file, how am I meant to know that the following is actually just a single record with 2 fields?

P Sherman, 42 Wallaby Way,
Sydney, AUSTRALIA

Whereas if it was quoted properly that would be obvious, i.e.

P Sherman, "42 Wallaby Way,
Sydney, AUSTRALIA"

FYI, here's the rules relating to quotes from RFC4180 (the MIME type definition for CSV).

5 Each field may or may not be enclosed in double quotes (however some programs, such as Microsoft Excel, do not use double quotes at all). If fields are not enclosed with double quotes, then double quotes may not appear inside the fields. For example:

   "aaa","bbb","ccc" CRLF
   zzz,yyy,xxx

6 Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes. For example:

   "aaa","b CRLF
   bb","ccc" CRLF
   zzz,yyy,xxx

7 If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote. For example:

   "aaa","b""bb","ccc"