RBL JAVA Tips
Main page
Home
[ Recherche | Home Page | Index Java tips ]


PrevRetour IndexNext
  • But...
    Utilisation de l'exemple de lecture du fichier pour lire le document html courant.

  • tipsRead.java : Source de l'applet
  • Exemple d utilisation
  • code
  • //****************************************************************************** 
    // ----------------------------------------------------------- 
    // tipsRead.java (same tipsFile2.java in JDK 1.1 version) 
    //   Read current html page 
    // ----------------------------------------------------------- 
    // Author : R. BERTHOU 
    // E-Mail : rbl@berthou.com 
    // URL    : http://www.javaside.com 
    // ----------------------------------------------------------- 
    // 1.00 * R.BERTHOU  * 09/04/2000 * samples tips 
    //****************************************************************************** 
    
     // Importations 
     import java.awt.* ; 
    
     import java.io.BufferedReader ; 
     import java.io.InputStreamReader ; 
     import java.io.IOException ; 
    
     public class tipsRead extends java.applet.Applet { 
         // Variables 
          String s ; 
          TextArea t1 = new TextArea("") ; 
    
         // Initialisation de l'applet 
         public void init() { 
              setLayout(new GridLayout(1,1)) ;   // Define a Layout 
              add(t1) ;                          // add TextArea 
              readFile() ;                       // read a file
              t1.setText(s) ;                    // Put text in a TextArea 
         } 
    
         // readFile 
         public void readFile() { 
              s = new String("") ; 
    
              // Define BufferedReader 
              BufferedReader fis = null ; 
    
              // Open Stream 
             try { 
                 fis = new BufferedReader( 
                          new InputStreamReader( getDocumentBase().openStream()) ) ; 
             } catch( IOException e ) { 
                 s = "readFile  Exception : " + e ; 
                 return ; 
             } 
    
             String sS = new String("") ; 
             // read loop 
             while ( true ) { 
              try { 
                   sS = fis.readLine() ; 
                   if (sS != null) 
                       s = s + "\r\n" + sS ; 
                   else 
                       break ; // end of file 
              } catch( IOException e ) { 
                   break ; 
              } 
             } 
             try { 
                  fis.close() ; 
             } catch( IOException e ) { } 
         } 
      } 
    

  • La recherche et l'ouverture du fichier est fait dans ce cas par un simple
    "getDocumentBase().openStream()".
  • Remarques : Ce code est relativement simple (meme très simple), et je n'ai toujours pas trouvé un interet à cela.



Copyright © 1996..2003, BERTHOU. Tous droits réservés.
Dernière modification le 03 Mars 2003 18H20

C.N.I.L.
n° 707410