Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package wdf;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InvalidObjectException;
- import java.io.ObjectInputStream;
- import java.io.ObjectInputValidation;
- import java.io.ObjectOutputStream;
- import java.io.ObjectStreamException;
- import java.io.Serializable;
- class Base implements Serializable {
- }
- class Sub extends Base {
- private Sub() {}
- public Sub(int g) {}
- int q = 9;
- }
- class Ser extends Sub implements Serializable, ObjectInputValidation {
- Ser() { super(7); }
- private void writeObject(java.io.ObjectOutputStream out) throws IOException {
- System.out.println("writeObject");
- out.defaultWriteObject();
- }
- private Object writeReplace() {
- System.out.println("writeReplace");
- return this;
- }
- int a = 8;
- float f = 9f;
- String s = "823982";
- private Object readResolve() throws ObjectStreamException {
- System.out.println("readResolve");
- return this;
- }
- private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
- System.out.println("readObject");
- in.registerValidation(this, 10);
- in.defaultReadObject();
- }
- public void validateObject() throws InvalidObjectException {
- System.out.println("jjj");
- // if (a != 9) {
- //
- // throw new InvalidObjectException("a != 9");
- // }
- }
- }
- public class My {
- public static void main(String[] args) {
- String fileName = "d:\\del.me";
- int bufSz = 8 * 1024;
- Ser obj = new Ser();
- try {
- ObjectOutputStream oos = new ObjectOutputStream(
- new BufferedOutputStream(new FileOutputStream(fileName), bufSz));
- oos.writeObject(obj);
- // oos.close();
- oos.flush();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Ser obj1 = null;
- Ser obj2 = null;
- try {
- ObjectInputStream ois = new ObjectInputStream(
- new BufferedInputStream(new FileInputStream(fileName), bufSz));
- ObjectInputStream ois1 = new ObjectInputStream(
- new BufferedInputStream(new FileInputStream(fileName), bufSz));
- obj1 = (Ser) ois.readObject();
- obj2 = (Ser) ois1.readObject();
- } catch (IOException | ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(obj1.a);
- System.out.println(obj2.a);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement