How to handle CSV files with an unknown number of columns using Super CSV

For a project, I need to deal with CSV files, where I do not know the columns before execution. The CSV files are absolutely correct, I just need to perform a simple task on several different files again and again. I need to analyze the values โ€‹โ€‹of the columns, so I will need to use the library to work with CSV files. For simplicity, suppose I need to do something simple, like adding a date column to all files, no matter how many columns they have. I want to do this with Super CSV because I use the library for other tasks.

What I'm struggling with is more of a conceptual problem. I am not sure how to handle files if I do not know in advance how many columns there are. I am not sure how I should define POJOs that display arbitrary CSV files or how I should define Cell processors if I do not know how many and how many columns there will be in the file. How can I dynamically create Cell processors that match the number of columns? How can I determine a POJO, for example, based on the header of a CSV file?

Consider the case when I have two CSV files: products.csv and address.csv. Suppose I want to add a date column with today's date for both files without having to write two different methods (e.g. addDateColumnToProduct () and addDateColumnToAddress ()) that do the same.

product.csv:

name, description, price
"Apple", "red apple from Italy","2.5โ‚ฌ" 
"Orange", "orange from Spain","3โ‚ฌ"

address.csv:

firstname, lastname
"John", "Doe"
"Coole", "Piet"

CSV, POJO, CSV ? Cell Processors? , , . product.csv

CellProcessor[] processor = new CellProcessor[] { 
    null,
    null,
    null
};

address.csv:

CellProcessor[] processor = new CellProcessor[] { 
    null,
    null
};

? , ?

1: , CSV . , CSV , .. POJO, , CSV . , csv.

@baba

private static void readWithCsvListReader() throws Exception {

        ICsvListReader listReader = null;
        try {
                listReader = new CsvListReader(new FileReader(fileName), CsvPreference.TAB_PREFERENCE);

                listReader.getHeader(true); // skip the header (can't be used with CsvListReader)
                int amountOfColumns=listReader.length();
                CellProcessor[] processor = new CellProcessor[amountOfColumns];
                List<Object> customerList;

                while( (customerList = listReader.read(processor)) != null ) {
                        System.out.println(String.format("lineNo=%s, rowNo=%s, customerList=%s", listReader.getLineNumber(),
                                listReader.getRowNumber(), customerList));
                }

        }
        finally {
                if( listReader != null ) {
                        listReader.close();
                }
        }
}
+3
2

, , ...

  CellProcessor[] processors=new CellProcessor[properties.size()];

  for(int i=0; i< properties.zise(); i++){
            processors[i]=new Optional();

   }
    return  processors;
+3

, , Super Csv:

http://supercsv.sourceforge.net/examples_reading_variable_cols.html

:

, read(), executeProcessors(). CSV , ( listReader.length()) .

+1

All Articles