Search code examples
javaopencsv

Deprecated CSVWriter constructor


I am using OpenCSV to create CSV files filled with records from database. I am using this:

        CSVWriter writer = new CSVWriter(
            new OutputStreamWriter(new FileOutputStream("example.csv"),
                    StandardCharsets.UTF_8), ';');

The problem is, this constructor is @Deprecated.

The library has several constructors, but only one is not deprecated. I am using this constructor:

/** @deprecated */
@Deprecated
public CSVWriter(Writer writer, char separator) {
    this(writer, separator, '"');
}

while I should be using this:

public CSVWriter(Writer writer, char separator, char quotechar, char escapechar, String lineEnd) {
    super(writer, lineEnd);
    this.escapechar = escapechar;
    this.quotechar = quotechar;
    this.separator = separator;
}

But I am not exactly sure what to put in there as I do not want my files to end up different.


Solution

  • The class already has static fields which represent the defaults, so you can simply do:

    CSVWriter writer = new CSVWriter(
        new OutputStreamWriter(new FileOutputStream("example.csv"), StandardCharsets.UTF_8),
        ';',
        CSVWriter.DEFAULT_QUOTE_CHARACTER,
        CSVWriter.DEFAULT_ESCAPE_CHARACTER,
        CSVWriter.DEFAULT_LINE_END
    );