0

I'm converting ResultSet object into csv format string using Univocity using below code:

`StringWriter stringWriter = new StringWriter();
CsvWriterSettings settings = new CsvWriterSettings();
settings.getFormat().setDelimiter('|');
settings.setQuoteAllFields(true);
settings.setEscapeUnquotedValues(true);
CsvRoutines csvRoutines = new CsvRoutines(settings);
csvRoutines.write(resultSet, stringWriter);
System.out.println(stringWriter.toString());`

What i'm trying to achieve here is to escape the special characters using setEscapeUnquotedValues(). But my result is not having the escaped values. Example: If the resultset contains 1|a,b then it should give the output as 1|a\,b

HyperioN
  • 3,433
  • 2
  • 22
  • 36
  • 1
    Your requirement doesn't sound right in the context of CSV escaping. With `settings.setQuoteAllFields(true);` all values will be written within quotes, so there won't be any need to escape pipes or commas inside the value. The only character requiring escaping in this case would be the quote character itself. Also the delimiter (pipe in your case) never needs to be escaped. It sounds like you are trying to generate TSV maybe, with pipes being used instead of tabs. – Jeronimo Backes May 15 '17 at 22:28
  • @JeronimoBackes You are correct, there was a misunderstanding regarding the requirement for using c style escaping and the pipe(delimiter) needs not be escaped. I just want to achieve escaping of special characters if there are any in the resultset. I tried using setEscapeUnquotedValues(), but it doesn't escapes the special characters. – HyperioN May 23 '17 at 07:36
  • You'd have to escape the characters by yourself as the definition of what is a "special character" depends on your specific requirements. – Jeronimo Backes May 23 '17 at 09:16
  • @JeronimoBackes By special character, I meant anything apart from a-z, A-Z and 0-9. – HyperioN May 23 '17 at 09:56
  • This means that there's thousands of potential characters to be escaped. You'll have to implement that by hand. – Jeronimo Backes May 23 '17 at 09:57

0 Answers0