java reading from csv file and storing its information into ArrayList

后端 未结 2 817

I\'m a java newbie and I need a some help

so here is my main method:

RegistrationMethods dmv = new RegistrationMethods();
ArrayList I         


        
2条回答
  •  感动是毒
    2020-12-06 19:56

    You could use something like this:

    ArrayList owners = new ArrayList();
    
    BufferedReader br = new BufferedReader(new FileReader(new File("path/to/your/file.csv")));
    String line;
    while ((line = br.readLine()) != null) {
    
        String[] entries = line.split(",");
    
        CarOwner owner = new CarOwner(entires[0], entries[1], entries[2], entries[3]);
    
        owners.add(owner);
    }
    

    Do you have a real csv file (all values separated by ,) or are they separated by spaces or something like that? In that case you'd have to replace the , with spaces.

提交回复
热议问题