Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

Lab 5

1. Modify the DataSetReader class to read BankAccounts from a file


bankAccountFile.
2. Modify the code in ATMManager to read the BankAccounts from a file. (You
can use the code in DataSetReader class for this).
3. Create a class which will take input of BankAccount details from a file
bankAccountDetailsFile and create the BankAccounts and save it in
bankAccountFile. (After doing I/O in class).
4. Create a file addressFile. Write all the addresses of the BankAccounts from
the array into the file addressFile.

Writing and reading objects from a file:


Object should be Serializable
import java.io.Serializable;
public class BankAccount implements Serializable{}
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
try{
FileOutputStream fout = new FileOutputStream("c:\\BAFile");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(bankaccount);
oos.close();
System.out.println("Done");
}catch(Exception ex){
ex.printStackTrace();
}

For reading use readObject() method of the ObjectInputStream. This will


return an Object, you will have to cast it before use.

You might also like