0

Problem:

I am using com.opencsv to parse a CSV file and get back an array of java objects. There is a method called setType() that unfortunately is deprecated. I have not been able to find an equivalent method.

Dependency:

  <dependency>
                <groupId>com.opencsv</groupId>
                <artifactId>opencsv</artifactId>
                <version>3.3</version>
            </dependency>

Spec (Javadoc):

Javadoc for version 3.6 (can't find 3.3): http://opencsv.sourceforge.net/apidocs/com/opencsv/bean/HeaderColumnNameMappingStrategy.html#setType(java.lang.Class)

My Code:

   private List<importedFromCSV> handleCSV(byte[] bytes,Class clazz) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(bais));

        ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
        strat.setType(clazz);
        //String[] columns = new String[] {"a","b"}; // the fields to bind do in your JavaBean
        //do we want csv to include columns or do we hardcode the order?
        CSVReader reader = new CSVReader(bufferedReader);
        String [] columns;
        if((columns = reader.readNext()) == null) {
            return null;
        }

        strat.setColumnMapping(columns);

        CsvToBean csv = new CsvToBean();
        return csv.parse(strat, bufferedReader);
    }

Related:

Example - Populating Javabean via openCSV - code explanation

Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • 1
    see this: http://stackoverflow.com/questions/29455470/what-does-this-mean-this-method-is-deprecated-as-the-user-should-use-the-java-5 – dgabriel Dec 01 '15 at 09:57
  • @Denis Gavrus How rude of them.... I guess a link that that page will go in a comment with the `@SuppressWarnings("deprecation")` annotation! – Menelaos Dec 01 '15 at 10:14

1 Answers1

3

Update to version 3.6. It is no longer deprecated.

It was deprecated in the early 3.X releases as we were trying to find a way to work with Generics (so getType would return T instead of what was passed in setType). When this proved impossible the code was backed out but the deprecation was accidentally left in.

Scott Conway
  • 975
  • 7
  • 13
  • Thanks for the answer. By the way, reading the javadoc I noticed something about reading headers. Is there a bettery way than the way I am doing it now to read the fields/columns in an array and then do CsvToBean? Thanks! – Menelaos Dec 01 '15 at 23:57
  • 1
    Yes - in the tests in opencsv there are some integration tests. Look at HeaderColumnNameMappingStrategyUserTest. Here is the main snippet of the test. private List createTestParseResult() throws FileNotFoundException { CSVReader reader = new CSVReader(new FileReader(USER_FILE)); HeaderColumnNameMappingStrategy strat = new HeaderColumnNameMappingStrategy(); strat.setType(MockUserBean.class); CsvToBean csv = new CsvToBean(); return csv.parse(strat, reader); } – Scott Conway Dec 20 '15 at 23:02