// Lire un fichier avec Scanner (import java.util.Scanner;) // ATTENTION LA CLASSE SCANNER N'EXISTE QUE DEPUIS JAVA 5.0 public static String lireFichier(String filePath) //lecture du fichier { String strLignes = ""; try { scanner = new Scanner(new File(filePath)); try { // On boucle sur chaque champ detecté while (scanner.hasNextLine()) { String line = scanner.nextLine(); // traitement à faire <<<<<<<<<<<<<<<<<<<<<<<<<<<< // par exemple: System.out.println(line); { } finally { // dans tous les cas, on ferme nos flux scanner.close(); } } catch (IOException ioe) { //e.printStackTrace(); System.out.println("Erreur --" + ioe.toString()); } return strLignes; }
lireFichier("C:\\unDossier\\ficxyz.txt"); // ou bien si le fichier est dans le dossier de travail: lireFichier("ficxyz.txt");
texte = texte.toLowerCase();
public class TaClasse... { --------------- -------------- private static Scanner scanner; --------------