Lire dans un fichier : passer à la ligne suivante

java versus hardtek Messages postés 18 Date d'inscription mardi 7 septembre 2004 Statut Membre Dernière intervention 17 juillet 2007 - 17 sept. 2004 à 02:25
HFanny Messages postés 699 Date d'inscription mercredi 19 février 2003 Statut Membre Dernière intervention 13 mai 2011 - 17 sept. 2004 à 08:53
Je veux lire toutes les lignes de ce fichier.

Mais je ne fait que lire la première ligne. Comment passer au suivantes ?

public int[] recuperer_standard_BUT ()
{

String adresse_du_fichier = System.getProperty("user.dir") + "\\standard_BUT.txt";
int tab_BUT[] = new int[32];
try
{
FileReader fr = new FileReader(adresse_du_fichier);
BufferedReader br = new BufferedReader(fr);
String texte = "";

for(int i = 0; i<32; i++)
{
texte = texte + br.readLine();
int j = Integer.valueOf(texte).intValue();
tab_BUT[i] = j;
}

br.close();

// conversion du texte récupéré en integer

}
catch(IOException ioe)
{
System.out.println("Erreur : " + ioe);
}

return tab_BUT;
}

1 réponse

HFanny Messages postés 699 Date d'inscription mercredi 19 février 2003 Statut Membre Dernière intervention 13 mai 2011 20
17 sept. 2004 à 08:53
Salut,
pour lire toutes les lignes d'un fichier une par une il faut boucler sur readLine() jusqu'à temps que ce que le résultat de readLine soit égal à null :

ligne = br.readLine();
i = 0;
while (ligne != null)
{
                texte = texte + ligne;
                int j = Integer.valueOf(texte).intValue;
tab_BUT[i] = j;
ligne = dis.readLine();
i++;
}


ou bien :
while( (ligne=br.readLine())!=null)
{
                texte = texte + ligne;
                int j = Integer.valueOf(texte).intValue;
tab_BUT[i] = j;
ligne = dis.readLine();
i++;
}


Fanny
0
Rejoignez-nous