Lire un fichier

Résolu
cs_amel2006 Messages postés 83 Date d'inscription samedi 21 janvier 2006 Statut Membre Dernière intervention 9 décembre 2006 - 27 mars 2006 à 22:15
cs_ducheseb Messages postés 344 Date d'inscription mardi 18 mai 2004 Statut Membre Dernière intervention 23 juin 2006 - 27 mars 2006 à 22:25
bonjour,
j'ai le programme suivant:


Code:
,
----

import java.io.*;

public class lirefichier {
public static void main (String []arg)
{
FileReader fr = new FileReader("temp/f1.txt");
int lu = fr.read();
if (lu != -1) {
char c = (char) lu;
System.out.println(c);
}

fr.close();

}
}

ce programme est fait pour lire un fichier et à la compilation il m'affiche des erreurs comme suit:


Code:
,
----

unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
FileReader fr = new FileReader("temp/f1.txt");
^
9: unreported exception java.io.IOException; must be caught or declared to be thrown
int lu = fr.read();
^
15: unreported exception java.io.IOException; must be caught or declared to be thrown
fr.close();
^
3 errors

Process completed.

j'ai pas comprie
et mon vrai problème et de lire le contenu de fichier jusqu'au arrivée à un mot donner
pouvez vous m'aider
merci


amel2006

1 réponse

cs_ducheseb Messages postés 344 Date d'inscription mardi 18 mai 2004 Statut Membre Dernière intervention 23 juin 2006 9
27 mars 2006 à 22:25
il suffit de mettre:

try{

FileReader fr = new FileReader("temp/f1.txt");
int lu = fr.read();
if (lu != -1) {
char c = (char) lu;
System.out.println(c);
}

fr.close();

}catch(java.io.IOException ioe){
System.out.println("Erreur d'accès au fichier");
}

"A game is a series of interesting choices." Sid Meier
3
Rejoignez-nous