java从csv文件读取并将其信息存储到ArrayList <class> </class>中

时间:2014-12-05 20:29:53

标签: java csv arraylist

我是一个java新手,我需要一些帮助

所以这是我的主要方法:

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

我有一个名为CarOwner的类,它有firstName, lastName, license, month, year个实例变量的getter和setter。

这是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;
}

2 个答案:

答案 0 :(得分:6)

您可以使用这样的方法并提供您想要读取的文件的路径。 这会创建一个Scanner来读取传入的文件。

它一次抓取每一行,并将新的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;
    }

答案 1 :(得分:1)

您可以使用以下内容:

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文件(所有值由,分隔),还是用空格或类似的东西分隔? 在这种情况下,您必须用空格替换。