【问题标题】:java reading from csv file and storing its information into ArrayList<class>java 从 csv 文件读取并将其信息存储到 ArrayList<class>
【发布时间】:2015-02-04 02:42:42
【问题描述】:

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

【问题讨论】:

标签: java csv arraylist


【解决方案1】:

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

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

附:我不知道您对 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;
    }

【讨论】:

    【解决方案2】:

    你可以这样使用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      相关资源
      最近更新 更多