3

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.

anon
  • 357
  • 5
  • 20
  • 2
    go through the documentation. no doubt the deprecated comment explains which alternative to use and how – Stultuske Aug 27 '18 at 09:25
  • 1
    so look at what the default values are for the other values, and pass those. But why not taking advantage of the additional options? – Stultuske Aug 27 '18 at 09:35

2 Answers2

11

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
);
Michael
  • 41,989
  • 11
  • 82
  • 128
2

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

anon
  • 357
  • 5
  • 20