2014-12-05 51 views
4

我是一個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://stackoverflow.com/questions/224952/most-concise-way-to-read-the-contents-of-a-file-input-stream-in-java。 – rajah9 2014-12-05 20:46:42

回答

5

你可以使用一個方法,像這樣提供您希望讀取的文件的路徑。 這會創建一個掃描器,以便從傳入的文件中讀取。

它每次抓取一行,並向結果數組中添加一個新的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; 
    } 
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文件(用,分隔的所有值),或者他們用空格或類似的東西分開嗎? 在這種情況下,您必須用空格來替換。

相關問題