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
|
//CREATION DU HASHTABLE
public static Hashtable<String,Agence> listeAgences= new Hashtable<String, Agence>();
public static String fich="test2.dat";
public static void main(String[] args)throws IOException{
//AJOUT DE 4 AGENCES DS LE HASHTABLE
Agence agence1 = new Agence("Marseille", "Mairie", "agence_marseille.dat");
Agence agence2 = new Agence("Saint-Ouen", "Carnot", "agence_stouen.dat");
Agence agence3 = new Agence("Lille", "Centre", "agence_lille.dat");
Agence agence4 = new Agence("Nice", "Plage", "agence_nice.dat");
String s1=agence1.getCode();
String s2=agence2.getCode();
String s3=agence3.getCode();
String s4=agence4.getCode();
listeAgences.put(s1, agence1);
listeAgences.put(s2, agence2);
listeAgences.put(s3, agence3);
listeAgences.put(s4, agence4);
//SERIALISATION
//public static void serialiser(Hashtable<String, ?> hash, String fichier) throws IOException{
FileOutputStream fos=null;
ObjectOutputStream myOut=null;
try{
fos= new FileOutputStream(fich);
myOut= new ObjectOutputStream(fos);
myOut.writeObject(listeAgences);
myOut.flush();
}
catch(FileNotFoundException fnfe){System.out.println("Le fichier mentionné n'a pu être trouvé !");
}
catch(IOException e1){System.out.println("Une erreur d'entrée sortie s'est produite !!");
}
finally{ if(myOut!=null){ myOut.close();}
}
System.out.println("Opération terminée (?)");
// }
//DESERIALISATION
FileInputStream fis=null;
ObjectInputStream myIn=null;
try{
fis=new FileInputStream(fich);
myIn= new ObjectInputStream(fis);
//PB DE CAST ICI : WARNING
Hashtable<String,Agence> sortie = (Hashtable<String, Agence>)myIn.readObject();
//énumération pour affichage;
Enumeration en = sortie.keys();
while(en.hasMoreElements()){
Object o= en.nextElement();
System.out.println( (sortie.get(o)).toString() );
} |
Partager