Rechercher
 
Notre page Facebook

Accueil KooR.fr
 |_ 
Accueil Langage Java
    |_ 
Java SE
       |_ 
java.io
          |_ 
ObjectOutputStream

Classe java.io.ObjectOutputStream

La classe ObjectOutputStream permet de sérialiser une grappe d'objets (de transformer en une séquence d'objets) dans un flux binaire (un stream).

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 
 88 
 89 
 90 
 91 
import java.awt.Point;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

/*
 * The java.io.Serializable interface implementation is required
 * if you want serialize your own instances based on your class.
 */
class Rational implements Serializable {
    private int numerator;
    private int denominator;
    
    public Rational() {
        this( 0, 1 );
    }
    
    public Rational( int numerator, int denominator ) {
        if ( denominator == 0 ) throw new RuntimeException( "Denominator cannot be null" );
        this.numerator = numerator;
        this.denominator = denominator;
    }
    
    @Override public String toString() {
        return "[" + this.numerator + "/" + this.denominator + "]";
    }
}



public class ObjectSerialization {    
    
    public static void serialize() {
        ArrayList<Object> myDataStructure = new ArrayList<Object>();
        myDataStructure.add( "A string instance" );
        myDataStructure.add( new Date() );
        myDataStructure.add( new Rational( 4, 5 ) );
        myDataStructure.add( 3.5 );
        
        try {
            FileOutputStream fos = new FileOutputStream( "Data.ser" );
            ObjectOutputStream oos = new ObjectOutputStream( fos );
            // No buffer is required because ObjectOutputStream is automaticaly buffered.
        
            oos.writeObject( myDataStructure );
            oos.writeObject( new Point( 10, 10 ) );   // Another instance
            
            oos.flush();
            oos.close();

            System.out.println( "Data are serialized" );
        } catch ( IOException exception ) {
            System.err.println( "Cannot write objects into the stream" );
            exception.printStackTrace();
        }
    }

    public static void unserialize() {
        try {
            FileInputStream fis = new FileInputStream( "Data.ser" );
            ObjectInputStream ois = new ObjectInputStream( fis );
            // No buffer is required because ObjectInputStream is automaticaly buffered.
        
            @SuppressWarnings( "unchecked" )
            ArrayList<Object> myDataStructure = (ArrayList<Object>) ois.readObject();
            Point location = (Point) ois.readObject();
            
            for( Object instance : myDataStructure ) {
                System.out.println( instance );
            }
            System.out.println( location );
            
            ois.close();
        } catch ( Exception exception ) {
            System.err.println( "Cannot read objects from the stream" );
            exception.printStackTrace();
        }
        
    }

    public static void main(String[] args) {
        ObjectSerialization.serialize();
        System.out.println( "---------------------------------------" );
        ObjectSerialization.unserialize();
    }
}
Exemple de sérialisation/déserialisation d'objets