Parsing CSV in java

后端 未结 9 1846
臣服心动
臣服心动 2020-11-27 21:12

I have this weird situation where I have to read horizontally. So I am getting a csv file which has data in horizontal format. Like below:

CompanyName,RunDat         


        
9条回答
  •  粉色の甜心
    2020-11-27 22:02

    You should really try univocity-parsers as its CSV parser comes with many features to handle all sorts of corner cases (unescaped quotes, mixed line delimiters, BOM encoded files, etc), which is also one of the fastest CSV libraries around.

    Simple example to parse a file:

    CsvParserSettings settings = new CsvParserSettings(); //heaps of options here, check the docs
    CsvParser parser = new CsvParser(settings);
    
    //loads everything into memory, simple but can be slow.
    List allRows = parser.parseAll(new File("/path/to/your.csv"));
    
    //parse iterating over each row
    for(String[] row : parser.iterate(new File("/path/to/your.csv"))){
        //process row here
    }
    
    //and many other possibilities: Java bean processing, column selection, format detection, etc.
    

    Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).

提交回复
热议问题