Prevent opencsv from writing quotes to .csv file

blue-sky picture blue-sky · Mar 12, 2013 · Viewed 18.9k times · Source

I'm populating a file from a resultSet like so :

      while(rs.next()){

          String[] entries = new String[3];
          entries[0] = rs.getString(1);
          entries[1] = ",";
          entries[2] = rs.getString(2);

          println("entries : "+entries);
          writer.writeNext(entries);

      }

When I open the excel file the values contain double quotes around them. So test1,test2,test3 when read from the database and written to a .csv file becomes "test1","test2","test3"

How can I write the text to file but not include the quotes ?

When I print the entries to the console the double quotes are not printed so I don't know where they are being added ?

Answer

obsessiveCookie picture obsessiveCookie · Mar 23, 2014

Just to extend on Matten's answer, use the built-in characters in the CSVWriter to specify the quotechar. So your writer would look like the following:

CSVWriter writer = new CSVWriter(new FileWriter(fileName), CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);