我是 Java 新手,我想要执行的功能是将文件中的一系列数据加载到我的 hashSet() 函数中。
问题是,我可以按顺序输入所有数据,但是我不能根据文件中的帐户名按顺序检索出来。
任何人都可以帮忙吗?
下面是我的代码:
public Set retrieveHistory(){ Set dataGroup = new HashSet(); 尝试{
File file = new File("C:\\Documents and Settings\\vincent\\My Documents\\NetBeansProjects\\vincenttesting\\src\\vincenttesting\\vincenthistory.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String data = br.readLine();
while(data != null){
System.out.println("This is all the record:"+data);
Customer cust = new Customer();
//break the data based on the ,
String array[] = data.split(",");
cust.setCustomerName(array[0]);
cust.setpassword(array[1]);
cust.setlocation(array[2]);
cust.setday(array[3]);
cust.setmonth(array[4]);
cust.setyear(array[5]);
cust.setAmount(Double.parseDouble(array[6]));
cust.settransaction(Double.parseDouble(array[7]));
dataGroup.add(cust);
//then proced to read next customer.
data = br.readLine();
}
br.close();
}catch(Exception e){
System.out.println("error" +e);
}
return dataGroup;
}
public static void main(String[] args) {
FileReadDataModel fr = new FileReadDataModel();
Set customerGroup = fr.retrieveHistory();
System.out.println(e);
for(Object obj : customerGroup){
Customer cust = (Customer)obj;
System.out.println("Cust name :" +cust.getCustomerName());
System.out.println("Cust amount :" +cust.getAmount());
}