Tec&Cult
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
Tec&Cult

Technologie et Culture
 
AccueilAccueil  PortailPortail  GalerieGalerie  Dernières imagesDernières images  RechercherRechercher  S'enregistrerS'enregistrer  Connexion  
Sujets similaires
Rechercher
 
 

Résultats par :
 
Rechercher Recherche avancée
Derniers sujets
» Que faire à Paris le week-end du 15 août ?
Serialize objects in java EmptyJeu 11 Aoû - 11:20 par Admin

» T-shirt Mec de Paname
Serialize objects in java EmptyMer 3 Aoû - 17:04 par Admin

» dating web site uk dating free parent single
Serialize objects in java EmptyMer 3 Aoû - 10:56 par Invité

» rsvp dating website seeking bisexual
Serialize objects in java EmptyLun 1 Aoû - 2:08 par Invité

» dating french woman gay bottom seeking tops
Serialize objects in java EmptyDim 31 Juil - 21:59 par Invité

» dating philippine woman man seeking wealthy woman
Serialize objects in java EmptyVen 29 Juil - 12:51 par Invité

» dating lesbian n r cacee cobb dating lachey nick
Serialize objects in java EmptyVen 29 Juil - 3:17 par Invité

» single dating chat room relationship dating advice
Serialize objects in java EmptyJeu 28 Juil - 0:21 par Invité

» скачать порно тетя скачать порнофото семейное
Serialize objects in java EmptyJeu 21 Juil - 14:34 par Invité

Navigation
 Portail
 Index
 Membres
 Profil
 FAQ
 Rechercher
Forum
Partenaires
Forum gratuit


Tchat Blablaland

Partagez | 
 

 Serialize objects in java

Voir le sujet précédent Voir le sujet suivant Aller en bas 
AuteurMessage
Admin
Admin


Messages : 156
Date d'inscription : 20/05/2010

Serialize objects in java _
MessageSujet: Serialize objects in java   Serialize objects in java EmptyJeu 23 Juin - 16:05

http://toptech.geekaddict.net/
Serialize objects in java
Serialization is the process of converting an object into a sequence of bits, so that it can be persisted on a storage medium such as a file or a memory buffer. This guide shows how to make a java class serializable and how to serialize/deserialize java objects and save/load them into files.
Our example class:
class Person {
private String firstName;
private String lastName;

public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
The only change we need is implementing the Serializable interface. This is an empty, tagging interface, no need to implement any additional methods:
import java.io.Serializable;

class Person implements Serializable {
private String firstName;
private String lastName;

public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
ObjectSerializer is an example class, shows how to perform object serialization in Java. The ObjectSerializer serializes or deserializes a serializable java object and write / read it to a file.
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

class ObjectSerializer {
public void serializeObject(String fileName, Object obj) {
ObjectOutputStream out = null;
try {
FileOutputStream fos = new FileOutputStream(fileName);
out = new ObjectOutputStream(fos);
out.writeObject(obj);
out.close();
} catch (IOException ex) {
throw new RuntimeException("Serialization failed", ex);
} finally {
try {
if(out != null) out.close();
} catch(Exception e) {
// do nothing
}
}

}

public Object deSerializeObject(String fileName) {
Object obj;
ObjectInputStream in = null;
try {
FileInputStream fis = new FileInputStream(fileName);
in = new ObjectInputStream(fis);
obj = in.readObject();
return obj;
}
catch (Exception e) {
throw new RuntimeException("De-serialization failed", e);
} finally {
try {
if(in != null)
in.close();
} catch(Exception e) {
// do nothing
}
}
}
}
Using the ObjectSerializer:
public class Test {
public static void main(String[] args) {
// writing to a file
Person person = new Person("John", "Doe");
ObjectSerializer serializer = new ObjectSerializer();
serializer.serializeObject("person.dat", person);

// reading from a file
Person personFromFile = (Person) serializer.deSerializeObject("person.dat");
}
}
Revenir en haut Aller en bas
http://toptech.geekaddict.net
 

Serialize objects in java

Voir le sujet précédent Voir le sujet suivant Revenir en haut 
Page 1 sur 1

Permission de ce forum:Vous ne pouvez pas répondre aux sujets dans ce forum
Tec&Cult :: Informatique :: Java :: Tutoriel-
Sauter vers: