/** * */ package file_manager; import java.io.BufferedReader; import java.io.FileReader; /** * @author Serpaud * */ public class ReaderFile { public static Exception Fin_Fichier; public static Exception Mot_Inconnue; private BufferedReader Id_File; private String Buffer; private String Name_File; /** * */ public ReaderFile(String Name_File) { this.Name_File = Name_File; } /* * Ouvre le fichier */ public void Open() throws Exception { try { Id_File = new BufferedReader(new FileReader(Name_File)); Buffer = Id_File.readLine(); } catch (Exception FileNotFoundException) { throw new Exception("Le fichier \"" + Name_File + "\" est introuvable."); } } /* * Ferme le fichier de configuration */ public void Close(){ try { if (Id_File != null) Id_File.close(); } catch (Exception e) { } } protected void finalize(){ Close(); } /* * Renvoie true si on est à la fin du fichier. */ public boolean EOF(){ return ((Buffer == null) || (Buffer.length() < 1)); } /* * Retourne la ligne courante du fichier ouvert */ public String Get_Line() throws Exception { String Tmp_Buffer = Buffer; if (!EOF()) Buffer = Id_File.readLine(); else throw Fin_Fichier; return Tmp_Buffer; } /* * retourne le "Niéme" mot d'un texte * le separateur est la tabulation */ public static String Word(String Texte, int Num) throws Exception { return Word(Texte, Num, '\t'); } /* * retourne le "Numiéme" mot d'un texte * le separateur est passé en paramètre */ public static String Word(String Texte, int Num, char Separateur) throws Exception { int First_index = Texte.indexOf(Separateur); if (Num == 1) if (First_index >= 0) return Texte.substring(0, First_index); else return Texte.toString(); else { if (First_index < 0 | Texte.length() <= 0) // Le texte ne contient pas le mot demandé. throw Mot_Inconnue; return Word(Texte.substring(First_index + 1), Num - 1, Separateur); } } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { ReaderFile Flux = new ReaderFile("Boitierx.cfg"); Flux.Open(); while(!Flux.EOF()){ System.out.println(Flux.Get_Line()); } Flux.Close(); } }