的java从csv文件读取和存储其信息到ArrayList中

问题描述:

我是一个Java新手,我需要一些帮助的java从csv文件读取和存储其信息到ArrayList中<class>

所以这里是我的主要方法:

RegistrationMethods dmv = new RegistrationMethods(); 
ArrayList<CarOwner> ItState = new ArrayList<CarOwner>(); 
dmv.processTextToArrayList(ItState); 

和我有一个类称为CarOwner并且它具有用于firstName, lastName, license, month, year实例变量的获取器和设置器。

这是我processTextToArrayList方法,方法头:

public void processTextToArrayList(ArrayList<CarOwner> inList) throws IOException 

这种方法应该是新CarOwner对象添加到inList CarOwner集合中通过CSV文件中的每一行,添加一个CarOwner对象到inList

我从csv文件到ArrayList中 我的CSV文件中读取包含类似:

Bunny Bugs ACB-123 5 2013 

Bunny Honey DEF-456 9 2013 

Bunny Lola GHI-789 3 2014 

如何使用while循环我的代码呢?

编辑:

我CarOwner类:

public class CarOwner extends Citizen implements CarOwnerInterface, Serializable 
{ 
private String license; 
private int month, year; 

public CarOwner() 
{ 
    super(); 
    license = "Not Assigned"; 
    month = 0; 
    year = 0;   
} 

public CarOwner(String inFirst, String inLast, String inLicense, int inMonth, int inYear) 
{ 
    super(inFirst, inLast); 
    license = inLicense; 
    month = inMonth; 
    year = inYear; 
} 

public void setLicense(String inLicense) 
{ 
    license = inLicense; 
} 

public String getLicense() 
{ 
    return license; 
} 

public void setMonth(int inMonth) 
{ 
    month = inMonth; 
} 

public int getMonth() 
{ 
    return month; 
} 

public void setYear(int inYear) 
{ 
    year = inYear; 
} 

public int getYear() 
{ 
    return year; 
} 

public int compareTo(Object o) 
{ 
    if ((o != null) && (o instanceof CarOwner)) 
    { 
     CarOwner otherOwner = (CarOwner) o; 
     if (otherOwner.compareTo(getYear()) > 0) 
      return -1; 
     else if (otherOwner.compareTo(getYear()) < 0) 
      return 1; 
     else if (otherOwner.equals(getYear())) 
      if (otherOwner.compareTo(getMonth()) > 0) 
       return -1; 
      else if (otherOwner.compareTo(getMonth()) < 0) 
       return 1; 
      else if (otherOwner.equals(getMonth())) 
       return 0; 
    } 
    return -1; 
} 

}

和我的市民阶层也:

public class Citizen implements CitizenInterface, Serializable 
{ 
private String firstName, lastName; 

public Citizen() 
{ 
    firstName = "No Name"; 
    lastName = "No Name"; 
} 

public Citizen(String inFirstName, String inLastName) 
{ 
    firstName = inFirstName; 
    lastName = inLastName; 
} 

public void setFirstName(String inFirst) 
{ 
    firstName = inFirst; 
} 

public String getFirstName() 
{ 
    return firstName; 
} 

public void setLastName(String inLast) 
{ 
    lastName = inLast; 
} 

public String getLastName() 
{ 
    return lastName; 
} 

public String toString() 
{ 
    String str; 

    str = firstName + " " + lastName; 

    return str; 
} 
+1

请尝试向我们展示CarOwner类。并请展开processTextToArrayList。它需要有一些内容从文件读取。看看http://*.com/questions/224952/most-concise-way-to-read-the-contents-of-a-file-input-stream-in-java。 – rajah9 2014-12-05 20:46:42

你可以使用一个方法,像这样提供您希望读取的文件的路径。 这会创建一个扫描器,以便从传入的文件中读取。

它每次抓取一行,并向结果数组中添加一个新的CarOwner(String,String,String,String,String)对象。

P.S.我不知道你的CarOwner的实现,所以我只用了所有的字符串...我会把它留给你弄清楚。

public ArrayList <CarOwner> processTextToCarOwnerList(String filePath) throws IOException { 
    ArrayList <CarOwner> result = new ArrayList <CarOwner>(); 
    Scanner scan = new Scanner(new File(filePath)); 
    while (scan.hasNextLine()) { 
     String line = scan.nextLine(); 
     String[] lineArray = line.split(" "); 
     result.add(new CarOwner(lineArray[0], lineArray[1], lineArray[2], lineArray[3], lineArray[4])); 
     } 
     return result; 
    } 

你可以使用这样的事情:

ArrayList<CarOwner> owners = new ArrayList<CarOwner>(); 

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); 
} 

你有一个真正的CSV文件(用,分隔的所有值),或者他们用空格或类似的东西分开吗? 在这种情况下,您必须用空格来替换。