Bonjour,
je suis en train de faire un petit test pour la sérialization / désérialization et j'ai un problème bizarre : malgré le fait que la sérialization se fasse correctement pour 2 objets (ou plus), la désérialization ne s'effectue que pour un objet).
En effet après la première lecture, j'obtiens une StreamCorruptedException (qui ne contient pas de message, mais juste la trace).
Voici la trace que j'obtiens sous Netbeans :
Et, bizarrement, une autre trace que j'obtiens sous Eclipse (même code mais dans des fichiers différentsjava.io.StreamCorruptedException
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:348)
at domain.Fichier.ReadCategories(Fichier.java:149)
at movies.AjoutDvdView.<init>(AjoutDvdView.java:34)
at movies.AjoutDvdView.getInstance(AjoutDvdView.java:41)
at movies.AjoutFilmView.initComponents(AjoutFilmView.java:42)
at movies.AjoutFilmView.<init>(AjoutFilmView.java:22)
at movies.AjoutFilmView.getInstance(AjoutFilmView.java:27)
at app.App.<init>(App.java:32)
at app.App$4.run(App.java:136)) :
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at tests.Fichier.ReadCategories(Fichier.java:51)
at tests.Test.main(Test.java:13)
Voici ma classe Categorie :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package tests; import java.io.*; public class Categorie implements Serializable { private static final long serialVersionUID = 1437025716169520864L; private String nom; private String description; public Categorie(String nom) throws NullPointerException, IllegalArgumentException { setNom(nom); } public Categorie(String nom, String description) throws NullPointerException, IllegalArgumentException { this(nom); setDescription(description); } public String getNom() { return nom; } public void setNom(String nom) throws NullPointerException, IllegalArgumentException { if (nom == null) throw new NullPointerException("Le nom de la catégorie doit être renseigné."); if (nom.compareTo("") == 0) throw new IllegalArgumentException("Le nom de la catégorie doit être renseigné."); this.nom = nom; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String toString() { return nom; } }
et la classe Fichier de lecture :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 package tests; import java.io.*; import java.util.*; import javax.swing.*; public class Fichier { public static String FICHIER_CATEGORIES = "categories.don"; private Fichier() { } private static void Save(Object obj, JLabel zoneMessage) { String fichierSauvegarde = Fichier.FICHIER_CATEGORIES; ObjectOutputStream sortie = null; try { sortie = new ObjectOutputStream( new BufferedOutputStream( new FileOutputStream(fichierSauvegarde, true))); sortie.writeObject((Categorie)obj); System.out.println("Ecrit : " + obj); } catch (IOException e) { System.out.println("IOException : " + e.getMessage()); } finally { if (sortie != null) try { sortie.close(); } catch (IOException ex) { } } } public static void SaveCategorie(Categorie categorie, JLabel zoneMessage) { Fichier.Save(categorie, null); } public static List<Categorie> ReadCategories(JLabel zoneMessage) { List<Categorie> listeCategories = new ArrayList<Categorie>(); ObjectInputStream entree = null; try { entree = new ObjectInputStream( new BufferedInputStream( new FileInputStream(Fichier.FICHIER_CATEGORIES))); while (true) { Object obj = entree.readObject(); if (obj instanceof Categorie) listeCategories.add((Categorie)obj); System.out.println("Lecture : " + (Categorie)obj); } } catch(EOFException e) { System.out.println("EOF : " + e.getMessage()); try { if (entree != null) entree.close(); } catch (IOException e2){ } } catch (ClassNotFoundException e) { System.out.println("ClassNotFoundException : " + e.getMessage()); if (zoneMessage != null) zoneMessage.setText("Erreur lors de la lecture des données."); } catch (IOException e) { System.out.println("IOException : " + e.getMessage()); e.printStackTrace(); if (zoneMessage != null) zoneMessage.setText("Erreur lors de la lecture des données."); } finally { if (entree != null) try { entree.close(); } catch (IOException ex) { } } return listeCategories; } }
Ensuite une classe de Test :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 package tests; public class Test { public static void main(String[] args) { Fichier.SaveCategorie(new Categorie("1", ""), null); Fichier.SaveCategorie(new Categorie("2", ""), null); Fichier.SaveCategorie(new Categorie("3", ""), null); System.out.println(Fichier.ReadCategories(null)); } }
Toute aide est la bienvenue
merci d'avance
Partager