how to read csv file without knowing header using java?

前端 未结 3 913
慢半拍i
慢半拍i 2020-12-22 09:15

i have to read CSV file in java, I googled it but i got the way to read using the headers; but i have no information of the column headers and number of columns available i

3条回答
  •  眼角桃花
    2020-12-22 10:10

    Casper datasets can do this is a few lines and will return a CDataRowSet which works in a similar way to a ResultSet. It can read the header from the file and return all Strings, eg:

    CBuilder builder = new CBuildFromFile(new File("people.csv"));
    CDataCacheContainer container = new CDataCacheContainer(builder);
    CDataRowSet cdrs = container.getAll();
    

    or it can also narrow the returned results to the smallest possible data type without losing fidelity. In doing so it can also handle missing integers and doubles (otherwise the resulting data type will remain as String), eg:

    CBuilder builder = new CBuildNarrowedFile(new File("people.csv")).setConvertMissing(true);
    CDataCacheContainer container = new CDataCacheContainer(builder);
    CDataRowSet cdrs = container.getAll();
    

提交回复
热议问题