Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
introduction de la seralisation
- Loading branch information
1 parent
749e6d4
commit e7ce34a
Showing
4 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 Franais"); | ||
Personne fh = new Personne("Franois", "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"); | ||
} | ||
} | ||
|
||
} |