java从csv文件中读取并将其信息存储到ArrayList<class>中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27324187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java reading from csv file and storing its information into ArrayList<class>
提问by cohsta
I'm a java newbie and I need a some help
我是一个 Java 新手,我需要一些帮助
so here is my main method:
所以这是我的主要方法:
RegistrationMethods dmv = new RegistrationMethods();
ArrayList<CarOwner> ItState = new ArrayList<CarOwner>();
dmv.processTextToArrayList(ItState);
and I have a class called CarOwner
and it has getters and setters for firstName, lastName, license, month, year
instance variables.
我有一个名为的类CarOwner
,它具有用于firstName, lastName, license, month, year
实例变量的getter 和 setter 。
And this is my method header for processTextToArrayList
method:
这是我的方法标题processTextToArrayList
:
public void processTextToArrayList(ArrayList<CarOwner> inList) throws IOException
this method is supposed to add new CarOwner
objects to the inList CarOwner
collection passed in. For each line of the csv file, a CarOwner
object is added to inList
.
此方法应该向传入CarOwner
的inList CarOwner
集合添加新对象。对于 csv 文件的每一行,将一个CarOwner
对象添加到inList
.
I have to read from csv file into arraylist my csv file contains something like:
我必须从 csv 文件读取到 arraylist 我的 csv 文件包含如下内容:
Bunny Bugs ACB-123 5 2013
Bunny Honey DEF-456 9 2013
Bunny Lola GHI-789 3 2014
how do I code this using while loop?
我如何使用while循环对此进行编码?
edit:
编辑:
my CarOwner class is :
我的 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;
}
}
}
and my Citizen class is also:
我的公民课也是:
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;
}
回答by austin wernli
You could use a method like this and provide the path to the file you wish to read from. This creates a Scanner to read from the file passed in.
您可以使用这样的方法并提供要读取的文件的路径。这将创建一个扫描器来读取传入的文件。
It grabs each line one at a time and adds a new CarOwner(String,String,String,String,String) object to the result array.
它一次抓取每一行,并将一个新的 CarOwner(String,String,String,String,String) 对象添加到结果数组中。
P.S. i have no idea your implementation of CarOwner so i just used all Strings... I'll leave that to you to figure out heh.
PS 我不知道你对 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;
}
回答by Phiwa
You could use something like this:
你可以使用这样的东西:
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);
}
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.
你有一个真正的 csv 文件(所有值都用 , 分隔)还是用空格或类似的东西分隔?在这种情况下,您必须将 , 替换为空格。