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
|
// Ecrit une Pers
public static void save( RandomAccessFile f, Pers p )throws Exception{
f.writeLong( p.getMatricule() ) ;
// Génération d'une suite de 30 char. complétés par des espaces à droite
f.writeChars( String.format("%-30s",p.getNom()) ) ;
f.writeBoolean( p.getMarie() ) ;
f.writeDouble( p.getTaille() ) ;
}
// Lit une Pers. Retourne null si EOF
public static Pers load( RandomAccessFile f ){
Pers p = null ;
try{
long matricule = f.readLong() ;
// Lecture de 30 char
char[] chars = new char[30] ;
for(int i=0;i<30;i++)
chars[i] = f.readChar() ;
String nom = new String(chars);
On enlève les espaces inutiles
nom = nom.trim() ;
boolean marie = f.readBoolean();
Double taille = f.readDouble();
p = new Pers(matricule,nom,marie,taille) ;
}
catch( Exception e ){
}
return p ;
}
public static long getSize(){
// long + chaine de 30 + booléen + double
return 8 + 2*30 + 1 + 8 ;
} |