Search code examples
javaregexcsvseparator

java csv semi colon issue


I have to modify a program. This program creates csv file with columns in database like this :

    csvBuilder.initCsvFile();
    csvBuilder.createRow(headers.toArray(new String[headers.size()]));
while (resultSet.next()) {
        String[] row = new String[resultSetColumnCount];
        int columnIndex = 1;
        while (columnIndex <= resultSetColumnCount) {
            Object object = resultSet.getObject(columnIndex);
            if (object == null) {
                row[columnIndex - 1] = "";
            } else {
                row[columnIndex - 1] = object.toString();
            }
            columnIndex++;
        }
        csvBuilder.createRow(row);
    }

initCsvFile method:

public void initCsvFile() {
    try {
        writer = new CSVWriter(new FileWriter(filePath), ';', CSVWriter.NO_QUOTE_CHARACTER);
    } catch (IOException e) {
        logger.error("CsvBuilder : error when creating {}", filePath, e);
        throw new CsvException("Error when creating the file : " + filePath, e);
    }
}

createRow method:

public void createRow(String[] row) {
    writer.writeNext(row);
}

The problem is that, i have some data that includes the separator ';' like shown below,

id col1     col2   
1   US     United;States

Unfortunately, the csv file separates United and states in two . How can I check the content of resultSet and escape the separator without modify all program to get United;states in one row?

Thanks for your help


Solution

  • In those cases, you will need some quote character, otherwise there will be ambiguity; in your case, you're instructing it to not use quotes, and as a result, it has no way of treating those cases.

    Try explicitly defining a quote character:

    public void initCsvFile() {
        try {
            writer = new CSVWriter(new FileWriter(filePath), ';', '"');
        } catch (IOException e) {
            logger.error("CsvBuilder : error when creating {}", filePath, e);
            throw new CsvException("Error when creating the file : " + filePath, e);
        }
    }
    

    Keep in mind, that in this case any data that could cause ambiguity will be enclosed between quotes:

    1;US;"United;States"
    

    That's valid CSV, and most CSV parsers will automatically strip the quotes when reading.