DMI's Blog sur les technos .NET(dotnet) et J2EE

Serialisation avec java

package blog.tecnoJava.serialization;

 

import java.beans.XMLDecoder;

import java.beans.XMLEncoder;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

 

public class Serialize {

 

            //Cette méthode permet d’écrire (sérialiser) un objet java dans un fichier

            //Binaire:*.dat,*.bin,...

            public void WriteInBinaryFormat(Object oToSerialize,String strDestinationPath) throws IOException{

                        //Create output streams.

                        FileOutputStream fos = new FileOutputStream(strDestinationPath);

                        ObjectOutputStream oos = new ObjectOutputStream(fos);

                        //Write object

                        oos.writeObject(oToSerialize);

            }

//          Cette Methode Permet de lire un objet sérialiser à partir d’un fichier binaire

            public Object ReadFromBinaryFile(String strSourceFilePath) throws IOException, ClassNotFoundException{

                        //Create input streams.

                        FileInputStream fis = new FileInputStream(strSourceFilePath);

                        ObjectInputStream ois = new ObjectInputStream(fis);

                        //Read object

                        return ois.readObject();

            }

           

            //Cette méthode permet d’écrire (archiver) un objet java dans une archive

            //XML comme ci-dessous

            public void WriteInXMLArchiveFormat(Object oToSerialize,String strDestinationPath) throws FileNotFoundException{

//                      Create output stream.

                        FileOutputStream fos = new FileOutputStream(strDestinationPath);

                        //Create XML encoder

                        XMLEncoder Xmlenc = new XMLEncoder(fos);

                        //Write object.

                        Xmlenc.writeObject(oToSerialize);

            }

            public Object ReadFromXMLArchive(String strSourceFilePath) throws FileNotFoundException{

//                      Create input stream.

                        FileInputStream fis = new FileInputStream(strSourceFilePath);

                        //Create XML decoder

                        XMLDecoder Xmldec = new XMLDecoder(fis);

                        //Read object.

                        return Xmldec.readObject();

            }

}

Vos commentaires

Aucun commentaire pour le moment.

Autres publications sur le sujet

Aucune référence pour le moment.

Cet article ne peut faire référence à d'autres publications.

Commenter cet article

Cet article ne peut être commenté.