Java - Splitting a CSV file into an Array

前端 未结 4 1564
别跟我提以往
别跟我提以往 2021-01-19 14:38

I have managed to split a CSV file based on the commas. I did this by placing a dummy String where ever there was a \',\' and then splitting based on the dummy String.

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-19 15:18

    As per the dummy strings you mentioned, it could be easily processed with the help of an existing library. I would like to recommand the open source library uniVocity-parsers, which procides simplfied API, significent performance and flexibility.

    Just refer to few lines of code to read csv data into memory with array:

    private static void parseCSV() throws FileNotFoundException {
        CsvParser parser = new CsvParser(new CsvParserSettings());
        List parsedData = parser.parseAll(new FileReader("/examples/example.csv"));
    
        for (String[] row : parsedData) {
            StringBuilder strBuilder = new StringBuilder();
            for (String col : row) {
                strBuilder.append(col).append("\t");
            }
            System.out.println(strBuilder);
        }
    }
    

提交回复
热议问题