reading a csv file into a array

后端 未结 3 1951
悲哀的现实
悲哀的现实 2020-12-14 23:14

I am trying to read the csv file, \"read_ex.csv\", into an array. I have searched endlessly on the web/stackoverflow to find a way to read the file into an array. The best i

3条回答
  •  没有蜡笔的小新
    2020-12-14 23:40

    I am new to java and wold be open to any method that reads a csv into a file that a beginner could understand.

    Here is an existing solution for you from Apache Commons, the Apache Commons CSV project. See the Apache Commons CSV parser guide.

    It is very easy to use out of the box.

    Here is a basic worfklow:

    • Create a class that has the fields that "match" the columns of csv file.
    • Each row in your csv is an object of that class with properties corresponding to the fields in your csv. Use the library to get each field and store it in your object in a loop.
    • In your loop, you will store the object with the fields/properties read from the csv in the ArrayList
    • After the loop ends, your ArrayList will have elements that "matches" the rows in your csv file. You are free to access any of the rows/elements in the list.

    If you are not familiar with how List and ArrayList works in Java, please refer to pretty much any Java tutorials online including the Oracle Tutorial.

    Search Stackoverflow for hundreds of posts about using Commons CSV.

提交回复
热议问题