I was able to save the contents in an ArrayList which contains objects to a file by using the PrintWriter,
Declaring
public static ArrayList<AccountRecords> userRecords = new
ArrayList<AccountRecords>();
Method
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;
}
}
AccountRecords
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;
}
But now i want to read the contents in the file userRecords
into the arrayList again, how can achieve this?
private static Boolean readObj(){
return true;
}
PS, the contents in the arrayList is inserted at a point in the program therefore the arrayList is not empty.