0

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.

Elinoter99
  • 599
  • 1
  • 7
  • 22
  • Take a look at https://stackoverflow.com/questions/17293991/how-to-write-and-read-java-serialized-objects-into-a-file – Thiyagu Feb 25 '18 at 14:54

1 Answers1

1

There's no way you can do this as-is. You don't even declare a toString() method that you could parse later. The simplest method would be to use an object stream.

Here's some example code. I write the data to a base 64 encoded string, but you could just as easily write the data to a file.

  // Create a base 64 encoded string and print it
  byte[] data = {-1,-2,-3,0,1,2,3,};
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ObjectOutputStream enc = new ObjectOutputStream( bos );
  enc.writeObject( data );
  enc.close();

  String b64 = Base64.getEncoder().encodeToString( bos.toByteArray() );
  System.out.println( "data=" + b64 );

  // Read some base 64 encoded data and print it
  String dataIn = "rO0ABXVyAAJbQqzzF/gGCFTgAgAAeHAAAAAH//79AAECAw==";
  byte[] bdataIn = Base64.getDecoder().decode( dataIn );
  ByteArrayInputStream bais = new ByteArrayInputStream( bdataIn );
  ObjectInputStream ois = new ObjectInputStream( bais );
  byte[] readData = (byte[]) ois.readObject();

  System.out.println( "orig array: " + Arrays.toString( readData ) );

To write an array list, just write it:

OutputStream os = new FileOutputStream(userRecordsFile);
ObjectOutputStream enc = new ObjectOutputStream( os );
enc.writeObject( userRecords );
enc.close();

Reverse the process to read the data in.

markspace
  • 10,621
  • 3
  • 25
  • 39