package elte.java2_utikalauz5.io; import java.io.*; /** Objektumok szerializációja. @link.forrásfájl {@docRoot}/../data/io/src ExterTeszter.java @link.letöltés {@docRoot}/../data/io ExterTeszter.jar @since Java 2 Útikalauz programozóknak 1.3 */ public class ExterTeszter { static class Datum implements Serializable { int év, hónap, nap; /** Verziószám */ private final static long serialVersionUID = 15L; private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField("dátum", int.class) }; private void writeObject( ObjectOutputStream out ) throws IOException { ObjectOutputStream.PutField adatok = out.putFields(); adatok.put("dátum",10000*év+100*hónap+nap); out.writeFields(); } private void readObject( ObjectInputStream in ) throws IOException, ClassNotFoundException { ObjectInputStream.GetField adatok = in.readFields(); int dátum = adatok.get("dátum",0); nap = (int) (dátum % 100); hónap = (int) ((dátum / 100) % 100); év = (int) (dátum / 10000); } public String toString() { return "Dátum: "+év+". "+hónap+". "+nap+"."; } } static class B extends Datum { /** Verziószám */ private final static long serialVersionUID = 15L; private void readObject( ObjectInputStream in ) throws IOException, ClassNotFoundException {} private void writeObject( ObjectOutputStream out ) throws IOException {} } static class C extends Datum implements Externalizable { /** Verziószám */ private final static long serialVersionUID = 15L; public C(){ super(); } public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException {} public void writeExternal( ObjectOutput out ) throws IOException {} } public static void main( String[] args ) throws IOException, ClassNotFoundException { Datum b = new B(); Datum c = new C(); ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("exter.dat") ); b.év = c.év = 2000; b.hónap = b.nap = c.év = c.nap = 1; out.writeObject(b); out.writeObject(c); out.close(); ObjectInputStream in = new ObjectInputStream( new FileInputStream("exter.dat") ); b = (B)in.readObject(); System.out.println(b); c = (C)in.readObject(); System.out.println(c); in.close(); } }