Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I give you a sample </p> <pre><code>import java.io.Serializable; public class Account implements Serializable { private int accountNo; private String custName; private int balance; /** Creates a new instance of Account */ public Account(int accNo, String name, int bal) { this.accountNo = accNo; this.custName = name; this.balance = bal; } @Override public String toString() { String str = "Account No:" + this.accountNo; str += "\nCustomer name:" + this.custName; str += "\nBalance:" + this.balance; return str; } } </code></pre> <p>Write and read object</p> <pre><code>package me.dev; import java.io.EOFException; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; public class Main { public void writeObject(ArrayList&lt;Object&gt; listAccount) throws IOException { //Create FileOutputStream to write file FileOutputStream fos = new FileOutputStream("C:\\bank.datum"); //Create ObjectOutputStream to write object ObjectOutputStream objOutputStream = new ObjectOutputStream(fos); //Write object to file for (Object obj : listAccount) { objOutputStream.writeObject(obj); objOutputStream.reset(); } objOutputStream.close(); } public ArrayList&lt;Account&gt; readObject() throws ClassNotFoundException, IOException { ArrayList&lt;Account&gt; listAccount = new ArrayList(); //Create new FileInputStream object to read file FileInputStream fis = new FileInputStream("C:\\bank.datum"); //Create new ObjectInputStream object to read object from file ObjectInputStream obj = new ObjectInputStream(fis); try { while (fis.available() != -1) { //Read object from file Account acc = (Account) obj.readObject(); listAccount.add(acc); } } catch (EOFException ex) { //ex.printStackTrace(); } return listAccount; } /** * @param args the command line arguments */ public static void main(String[] args) throws ClassNotFoundException { try { // TODO code application logic here ArrayList&lt;Object&gt; listAcc = new ArrayList&lt;Object&gt;(); listAcc.add(new Account(1, "John", 1000)); listAcc.add(new Account(2, "Smith", 2000)); listAcc.add(new Account(3, "Tom", 3000)); Main main = new Main(); main.writeObject(listAcc); ArrayList&lt;Account&gt; listAccount = main.readObject(); System.out.println("listisze:" + listAccount.size()); if (listAccount.size() &gt; 0) { for (Account account : listAccount) { System.out.println(((Account) account).toString()); } } } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload