Search code examples
javacsvopencsv

CSVWriter is removing spaces on string?


I'm working on a java project and I'm trying to use the library "opencsv" to make a CSV file. The problem is this lib is removing spaces between words in a string.

Example:

StringWriter sw = new StringWriter();
CSVWriter csv = new CSVWriter(sw, ';');
String[] header = {"COLUMN_1", "COLUMN_2", "COLUMN_3"};
csv.writeNext(header);

String[] row_1 = {"1", "word with spaces on it", "1234"};
csv.writeNext(row_1);

csv.close();

System.out.println(sw.toString());

Output:

"COLUMN_1";"COLUMN_2";"COLUMN_3"
"1";"wordwithspacesonit";"1234"

Expected output:

"COLUMN_1";"COLUMN_2";"COLUMN_3"
"1";"word with spaces on it";"1234"

I've looked for this problem but I have not found a solution for it... Somebody know how to solve this problem? Thank you


Solution

  • I replaces blank spaces for %20 and it worked.

    Example:

    System.out.println(sw.toString().replace(" ", "%20"));