【发布时间】:2018-02-25 14:41:47
【问题描述】:
我能够使用 PrintWriter 将包含对象的 ArrayList 中的内容保存到文件中,
声明
public static ArrayList<AccountRecords> userRecords = new
ArrayList<AccountRecords>();
方法
private static Boolean saveObj(){
try{
File userRecordsFile = new File("userRecords");
PrintWriter pw2 = new PrintWriter(new FileOutputStream(userRecordsFile));
for(int x=0;x<userRecords.size();x++){
pw2.println(userRecords.get(x));
}
pw2.close();
return true;
}catch(Exception e){
e.printStackTrace();
return true;
}
}
账户记录
class AccountRecords{
private String identificationNo;
private int accountNo;
private int monthId;
private double balance;
private double credit;
private double debit;
private int year;
public AccountRecords(String identificationNo, int accNo,int monthId, double balance, double creditAmount, double debitAmount, int year){
this.identificationNo = identificationNo;
this.accountNo = accNo;
this.balance = balance;
this.monthId = monthId;
this.credit = creditAmount;
this.debit = debitAmount;
this.year = year;
}
public double getCredit() {
return credit;
}
public double getDebit() {
return debit;
}
public double getBalance() {
return balance;
}
public String getIdentificationNo() {
return identificationNo;
}
public int getAccountNo() {
return accountNo;
}
public int getMonthId() {
return monthId;
}
public int getYear() {
return year;
}
但是现在我想再次将文件userRecords中的内容读入arrayList,如何实现呢?
private static Boolean readObj(){
return true;
}
PS,arrayList中的内容被插入到程序中的某个点,因此arrayList不为空。
【问题讨论】:
标签: java arrays arraylist printwriter