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 57 58 59 60 61 62 63 64 65
|
public class Serializer {
private static final Serializer instance = new Serializer();
private final String chemin = "puissance4.txt";
private Object IHM;
IHM i;
private Serializer() {
super();
}
public void serialiser(ArrayList<IHM> ihm){
FileOutputStream fichier=null;
ObjectOutputStream objet=null;
try {
fichier = new FileOutputStream(chemin);
objet = new ObjectOutputStream(fichier);
objet.writeObject(IHM);
objet.flush();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if(objet!=null){
objet.close();
}
if(fichier!=null){
fichier.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public ArrayList<IHM> deserialiser(){
ArrayList<IHM> res = null;
try {
FileInputStream fichier = new FileInputStream(chemin);
ObjectInputStream object = new ObjectInputStream(fichier);
res = (ArrayList<IHM>) object.readObject();
}
catch (java.io.IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
i.setVisible(true);
return res;
}
public static Serializer getInstance() {
return instance;
}
} |
Partager