Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
introduction de la seralisation
  • Loading branch information
guillaumemoreau committed Oct 15, 2012
1 parent 749e6d4 commit e7ce34a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Contact.java
@@ -1,8 +1,10 @@
import java.io.Serializable;

/**
* une classe abstraire Contact
* @author moreau
*
*/
public abstract class Contact {
public abstract class Contact implements Serializable {
public abstract String getContactInfo();
}
8 changes: 7 additions & 1 deletion Entreprise.java
@@ -1,12 +1,18 @@
/**
*
*/
import java.io.*;

/**
* @author moreau
*
*/
public class Entreprise implements Contactable {
public class Entreprise implements Contactable,Serializable {
/**
*
*/
private static final long serialVersionUID = 3433057254936104561L;

/**
* adresse mail
*/
Expand Down
9 changes: 8 additions & 1 deletion Personne.java
@@ -1,3 +1,5 @@
import java.io.Serializable;

/**
* Ceci est le fichier de ma classe Personne
*/
Expand All @@ -6,7 +8,12 @@
* @author moreau
* une classe Personne qui va dŽfinir les informations relatives ˆ une Personne
*/
public class Personne implements Contactable {
public class Personne implements Contactable,Serializable {
/**
* numŽro de sŽrie de la classe
*/
private static final long serialVersionUID = 433086348;

Contact adresse;

/**
Expand Down
59 changes: 59 additions & 0 deletions TestSerialisation.java
@@ -0,0 +1,59 @@
import java.io.*;

public class TestSerialisation {

public static void sauvePersonne(Personne p,String fileName) throws java.io.IOException {
FileOutputStream fOut = new FileOutputStream(fileName);
ObjectOutputStream oOut = new ObjectOutputStream(fOut);
oOut.writeObject(p);
oOut.flush();
oOut.close();
fOut.close();
}

public static Personne chargePersonne(String fileName) throws java.io.IOException, ClassNotFoundException {
FileInputStream fichier = new FileInputStream(fileName);
ObjectInputStream is = new ObjectInputStream(fichier);
Personne p = (Personne) is.readObject();
return p;
}
/**
* @param args
*/
public static void main(String[] args) {
Entreprise gouv = new Entreprise("Gouvernement Franais");
Personne fh = new Personne("Franois", "Hollande",true,1939);
Manager dd = new Manager("Dagot","Dimitri",true,1960,3243,gouv);
dd.setContact(new ContactMail("dimitri.dagot@ecp.fr"));

Employe gm = new Employe("Moreau","Guillaume",true,1973,0,gouv);
gm.setContact(new ContactMail("guillaume.moreau@ec-nantes.fr"));

Employe yl = new Employe("Yu","Lei",true,1973,467,gouv);
Employe yc = new Employe("Yin","Chuantao",true,1977,9,gouv);
dd.ajouteEmploye(gm);
dd.ajouteEmploye(yl);
dd.ajouteEmploye(yc);

try {
sauvePersonne(fh,"holland.ser");
sauvePersonne(gm,"gm.ser");
sauvePersonne(dd,"dd.ser");

Personne p1 = chargePersonne("holland.ser");
Employe egm = (Employe) chargePersonne("gm.ser");

System.out.println(p1.getNomComplet());
System.out.println(egm.getNomComplet());


}
catch(IOException e) {
System.out.println("Erreur d'Žcriture sur le disque : "+e.getMessage());
}
catch(ClassNotFoundException e) {
System.out.println("Classe inconnue");
}
}

}

0 comments on commit e7ce34a

Please sign in to comment.