Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deprecated CSVWriter constructor

Tags:

java

opencsv

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.

like image 251
anon Avatar asked Sep 06 '25 03:09

anon


2 Answers

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
);
like image 191
Michael Avatar answered Sep 07 '25 16:09

Michael


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".

like image 30
anon Avatar answered Sep 07 '25 17:09

anon