Doesn't this feature need the specific throws clauses ?
Code: Select all
private void writeObject(ObjectOutputStream out) throws IOException;
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;Moderator: admin
Code: Select all
private void writeObject(ObjectOutputStream out) throws IOException;
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;exact signatures - that's just method name and parameter types and orderClasses that require special handling during the serialization and deserialization process
must implement special methods with these exact signatures
Code: Select all
Exception in thread "main" java.io.NotActiveException: not in call to writeObject
	at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:436)
	at ocp.wrongq.Portfolio.writeObject(Portfolio.java:29)
	at ocp.wrongq.Portfolio.main(Portfolio.java:46)
Code: Select all
// In Portfolio.java
package ocp.wrongq;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Bond {
	String ticker = "bac";
	double coupon = 8.3;
	@Override
	public String toString() {
		return "Bond [ticker=" + ticker + ", coupon=" + coupon + "]";
	}
}
public class Portfolio extends Bond implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2252868398846453877L;
	String accountName;
	transient Bond bond = new Bond();
	private void writeObject(ObjectOutputStream os) throws Exception {
		os.defaultWriteObject();
		os.writeObject(bond.ticker);
		os.writeDouble(bond.coupon);
	}
	private void readObject(ObjectInputStream os) throws Exception {
		os.defaultReadObject();
		this.bond = new Bond();
		bond.ticker = (String) os.readObject();
		bond.coupon = os.readDouble();
		System.out.println(this.bond);
	}
	public static void main(String[] args) throws Exception{
		Portfolio p = new Portfolio();
		FileOutputStream fos = new FileOutputStream("t.tmp");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		p.writeObject(oos);
		FileInputStream fis = new FileInputStream("t.tmp");
		ObjectInputStream ios = new ObjectInputStream(fis);		
		p.readObject(ios);
	}
}
bortnikov_sa@mail.ru wrote: ↑Thu Dec 06, 2018 5:55 amHellow. Is it ok to use: os.writeObject(accountName) and accountName = (String) os.readObject() instead os.defaultWriteObject() and os.defaultReadObject() respectively?
I don't see a reason why calling os.writeObject(accountName) instead of os.defaultWriteObject() and this.accountName = (String) os.readObject() instead of os.defaultReadObject() not woking.admin wrote: ↑Thu Dec 06, 2018 9:54 amNo, both the methods have different purpose. defaultReadObject can only be called from within the implementation of readObject. Please take look at their API description.
Code: Select all
class Bond {
    String ticker = "bac";
    double coupon = 8.3;
}
class Portfolio implements Serializable {
    String accountName;
    transient Bond bond = new Bond();
    private void writeObject(ObjectOutputStream os) throws Exception {
        os.writeObject(accountName);
        os.writeObject(bond.ticker);
        os.writeDouble(bond.coupon);
    }
    private void readObject(ObjectInputStream os) throws Exception {
        this.accountName = (String) os.readObject();
        this.bond = new Bond();
        bond.ticker = (String) os.readObject();
        bond.coupon = os.readDouble();
    }
}Users browsing this forum: No registered users and 12 guests