Recherche séquentielle, certaines partie de code que je ne comprends pas
Bonjour tout le monde,
J'ai un programme qui fait une recherche séquentielle et qui quand a trouvé me donne un taux.
Il y'a certaines lignes que je ne comprends pas, comme celles-ci :
Code:
eof=(raf.length()==0);
eof est de type boolean et on lui affecte raf.length()==0
Je ne comprends pas cette ligne non plus :
Code:
eof= (raf.getFilePointer()==raf.length());
voici le code en entier :
Merci d'avance pour votre aide.
beegees
Code:
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
| import java.io.*;
public class FichierAccessDirect
{
public static void main(String[]args)throws IOException
{
File f=new File("c:/revisionRaf.pit");
f.delete();
RandomAccessFile raf=new RandomAccessFile (f,"rw");
raf.writeChars("LI");raf.writeDouble(0.45);
raf.writeChars("DI");raf.writeDouble(0.60);
raf.writeChars("CD");raf.writeDouble(0.75);
raf.writeChars("DV");raf.writeDouble(0.80);
long tailleRec=12;// un char = 2octets double 8
String categorie;
System.out.println("pour quel categorie voulez-vous un taux? LI, DI, CD, DV.");
categorie=Clavier.lireString();
System.out.println(lireFichier(raf,tailleRec));
System.out.println("Voici le taux pour "+categorie+" "+getTaux(raf,tailleRec,categorie));
}
public static String lireFichier(RandomAccessFile raf, long tailleRec)throws IOException
{
long nbRec=raf.length()/tailleRec;
char [] chars=new char[2];
String s="Categorie\ttaux\n";
raf.seek(0);
//System.out.println(s);
for(int i=0;i<nbRec;i++)
{
chars[0]=raf.readChar();chars[1]=raf.readChar();double taux=raf.readDouble();
s+=("\t"+chars[0]+chars[1]+"\t"+taux+"\n");
}
return s;
}
public static double getTaux(RandomAccessFile raf,long tailleRec, String categorie)throws IOException
{
boolean trouve,eof;//eof pour end of file
double taux=0;
char [] chars=new char[2];
long nbRec=raf.length()/tailleRec;
raf.seek(0);
trouve=false;
//on test si la valeur de eof est == à 0
eof=(raf.length()==0);
System.out.println("eof vaut "+eof);
while((!eof)&&(!trouve))
{
chars[0]=raf.readChar();
chars[1]=raf.readChar();
String s=new String (chars);//transforme un tableau de caractere en string
taux=raf.readDouble();
trouve=categorie.equals(s);
eof= (raf.getFilePointer()==raf.length());
}
if(trouve)
{
return taux;
}
else
{
taux=0;
return taux;
}
}
} |