Bonjour !
Alors j'ai un petit soucis avec les fichiers à accès aléatoire, mon programme est censé construire un fichier structuré à partir d'un vecteur contenant des objets de type Employe.
Visiblement il écrit correctement dans le fichier, puisque les informations contenues dans le vecteur s'y trouvent, mais au moment de la lecture, il lit correctement les entiers et les doubles, mais pas les chaines de caractères pour lesquelles j'ai des symboles qui s'affichent.
Je vous mets mes codes ci-dessous afin que vous puissiez me venir en aide :
La classe Employe :
La classe Annuaire (qui crée le fichier et le lit)
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 public class Employe { private int numero; private String nom; private String adresse; private String telephone; private double salaire; public Employe(int numero, String nom, String adresse, String telephone, double salaire){ this.numero = numero; this.nom = nom; this.adresse = adresse; this.telephone = telephone; this.salaire = salaire; } public void setNumero(int num){ numero = num; } public int getNumero(){ return numero; } public void setNom(String name){ nom = name; } public String getNom(){ return nom; } public void setAdresse(String ad){ adresse = ad; } public String getAdresse(){ return adresse; } public void setTelephone(String tel){ telephone = tel; } public String getTelephone(){ return telephone; } public void setSalaire(double sal){ salaire = sal; } public double getSalaire(){ return salaire; } public void afficher(){ System.out.println("Numero : " + numero); System.out.println("Nom du client : " + nom); System.out.println("Adresse du client : " + adresse); System.out.println("Telephone du client : " + telephone); System.out.println("Salaire : " + salaire); } }
La classe TestAnnuaire qui permet de tester les deux classes précédentes
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 import java.util.Vector; import java.util.Iterator; import java.io.*; public class Annuaire { private Vector<Employe> collectionEmployes; private RandomAccessFile fichierAnnuaire; private int nbEmployes; public Annuaire(Vector<Employe> cemp, RandomAccessFile rf){ collectionEmployes = cemp; fichierAnnuaire = rf; nbEmployes = 0; } public Annuaire(Vector<Employe> cemp){ collectionEmployes = cemp; nbEmployes = 0; try { fichierAnnuaire = new RandomAccessFile("annuaire.dat","rw"); } catch (IOException e){ System.out.println("PB création fichier annuaire dans Constructeur" + e.getMessage()); } } private String formatChaine(String chaine, int longueur){ StringBuffer chaineRes = new StringBuffer(chaine); if(chaineRes.length()>longueur) chaineRes.setLength(longueur); else { for (int i = chaineRes.length();i<longueur;i++){ chaineRes.append(" "); } } return (chaineRes.toString()); } public void creerAnnuaire() throws IOException{ Iterator it = collectionEmployes.iterator(); while(it.hasNext()){ Employe emp = (Employe)it.next(); try{ fichierAnnuaire = new RandomAccessFile("annuaire.dat","rw"); fichierAnnuaire.seek(fichierAnnuaire.length()); fichierAnnuaire.writeInt(emp.getNumero()); fichierAnnuaire.writeBytes(formatChaine(emp.getNom(),20)); fichierAnnuaire.writeBytes(formatChaine(emp.getAdresse(),40)); fichierAnnuaire.writeBytes(formatChaine(emp.getTelephone(),10)); fichierAnnuaire.writeDouble(emp.getSalaire()); nbEmployes++; } catch(IOException e){ System.out.println("Erreur IO : " + e.getMessage()); } } System.out.println("Annuaire créé avec succès !!!"); } private String lireChaine(int nbcars) throws IOException{ StringBuffer chaine = new StringBuffer(""); try{ for(int i=0;i<(nbcars/2);i++){ chaine.append(fichierAnnuaire.readChar()); } } catch(IOException e){ System.out.println("Erreur IO : " + e.getMessage()); } return (chaine.toString()); } private Employe lireUnEmploye(int pos) throws IOException{ Employe emp = new Employe(0,"","","",0.0); try{ fichierAnnuaire = new RandomAccessFile("annuaire.dat","r"); System.out.println("lecture de l'enregistrement à la position" +pos); fichierAnnuaire.seek(pos*152); emp.setNumero(fichierAnnuaire.readInt()); emp.setNom(lireChaine(20)); emp.setAdresse(lireChaine(40)); emp.setTelephone(lireChaine(10)); emp.setSalaire(fichierAnnuaire.readDouble()); fichierAnnuaire.close(); } catch(IOException e){ } return emp; } public void afficherEmploye(int pos) throws IOException{ Employe emp = new Employe(0,"","","",0.0); try{ emp = lireUnEmploye(pos); emp.afficher(); } catch(IOException e){ } } public int getNbEmploye(){ return nbEmployes; } }
Je vous remercie d'avance pour vos aides
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 import java.util.*; import java.io.*; public class TestAnnuaire { public static void main(String[] args){ try{ RandomAccessFile rf = new RandomAccessFile("annuaire.dat","rw"); Vector<Employe> cemp = new Vector<Employe>(); Employe e1 = new Employe(1,"Sidhoum","Rue des pissenlits","0125252525",25.0); cemp.add(e1); Employe e2 = new Employe(2,"Graver","Rue des tulipes","0125252525",1000000.0); cemp.add(e2); Employe e3 = new Employe(3,"Prince","Rue de paris","0166960212",0.0); cemp.add(e3); Employe e4 = new Employe(1,"Chevalier","Place monge","0145454545",10.0); cemp.add(e4); Employe e5 = new Employe(1,"Henri","Rue des saints pères","0152528545",5000.0); cemp.add(e5); Annuaire annuaire = new Annuaire(cemp,rf); annuaire.creerAnnuaire(); System.out.println("Nombre total d'employés dans l'annuaire : " + annuaire.getNbEmploye()); annuaire.afficherEmploye(0); annuaire.afficherEmploye(1); annuaire.afficherEmploye(2); } catch(FileNotFoundException e){ System.out.println("Dans main TestAnnuaire : " + e.getMessage()); } catch(IOException e){ System.out.println("Dans main TestAnnuaire : " + e.getMessage()); } } }![]()
Partager