anon
anon

Reputation: 357

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.

Upvotes: 4

Views: 4160

Answers (2)

Michael
Michael

Reputation: 44240

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
);

Upvotes: 12

anon
anon

Reputation: 357

This is the same constructor with default values:

CSVWriter writer = new CSVWriter(
            new OutputStreamWriter(new FileOutputStream(SAZKOVA_UDALOST),
                    StandardCharsets.UTF_8), ';', '"', '"', "\n");

Default value for separator is ;. Default value for quotechar is ". Default value for escapechar is ". Default value for lineEnd is "\n".

Upvotes: 2

Related Questions