HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"The Data Series"
Vol. 3, Issue 2, p. 52

	

Listing 1: Storing data based on parameter.
 
  public class ini extends java.lang.Object { 
       
     private String  FileName = null; 
     private Hashtable   data = null; 
       
     public ini( String _filename ) throws Exception; 
   
     public String get( String _parameter ); 
     public void   set( String _parameter, String _value ); 
  
     private boolean reloadData( BufferedReader InFile ); 
     private boolean writeData( PrintWriter OutFile); 
  } 

Listing 2: Opening & parsing the INI file.
 
  private boolean reloadData(BufferedReader InFile){ 
      String LineIn=""; 
      String key, value; 
      int c1; 
      data = new Hashtable(); 
   
      try{ 
          while ( (LineIn=InFile.readLine()) != null ){ 
               
              if ( LineIn.length() == 0 || LineIn.charAt(0) == '#') 
                  continue; 
               
              try{ 
                  c1  = LineIn.indexOf("="); 
                  key = LineIn.substring(0,c1).toLowerCase(); 
                  value = LineIn.substring(c1+1,LineIn.length() ); 
                  data.put(key,value); 
              }catch(Exception E){} 
          } 
   
          return true; 
      }catch(Exception E){} 
      return false; 
   } 
  

Listing 3: Get ( ) method.
 
  public String get( String _parameter ){ 
       
      if ( data.containsKey( _parameter ) ) 
          return (String)data.get(_parameter); 
      else 
          return null; 
  } 

Listing 4: Setting a new value.
 
  public void set( String _parameter, String _value ){ 
       
      if ( data.containsKey( _parameter ) ){ 
          data.remove( _parameter ); 
      } 
       
      data.put(_parameter,_value); 
      try{ 
          writeData( new PrintWriter(new BufferedWriter(new FileWriter(FileName))) ); 
      }catch(Exception E){} 
  } 

Listing 5: Writing the parameter to the file.
 
  private boolean writeData(PrintWriter _Out){ 
      Enumeration E = data.keys(); 
      String key; 
      while (E.hasMoreElements()){ 
          key = (String)E.nextElement(); 
          _Out.println( key + "=" + (String)data.get(key) ); 
      } 
      _Out.flush(); 
      return true; 
  } 

Listing 6: Complete listing.
 
 import java.util.*; 
 import java.io.*;  
   
 public class ini extends Object { 
        
      private String  FileName = null; 
      private Hashtable   data = null; 
       
      public ini( String _filename ) throws Exception 
      { 
          FileName = _filename; 
          BufferedReader InFile = new BufferedReader(new FileReader(_filename)); 
   
          if ( reloadData( InFile ) == false ) 
              throw new Exception("File created an error:" + FileName ); 
      } 
   
      public String get( String _parameter ){ 
           
          if ( data.containsKey( _parameter ) ) 
              return (String)data.get(_parameter); 
          else 
              return null; 
      } 
   
      public void set( String _parameter, String _value ){ 
           
          if ( data.containsKey( _parameter ) ){ 
              data.remove( _parameter ); 
          } 
           
          data.put(_parameter,_value); 
          try{ 
             writeData( new PrintWriter(new BufferedWriter(new FileWriter(FileName))) ); 
          }catch(Exception E){} 
      } 
   
      private boolean writeData(PrintWriter _Out) 
      { 
          Enumeration E = data.keys(); 
          String key; 
          while (E.hasMoreElements()){ 
              key = (String)E.nextElement(); 
              _Out.println( key + "=" + (String)data.get(key) ); 
          } 
          _Out.flush(); 
          return true; 
      } 
   
      private boolean reloadData(BufferedReader InFile) 
      { 
          String LineIn=""; 
          String key, value; 
          int c1; 
          data    = new Hashtable(); 
   
          try{ 
              while ( (LineIn=InFile.readLine()) != null ){ 
                   
                  if ( LineIn.length() == 0 || LineIn.charAt(0) == '#') 
                     continue; 
   
                  c1  = LineIn.indexOf("="); 
                  key = LineIn.substring(0,c1).toLowerCase(); 
                  value = LineIn.substring(c1+1,LineIn.length() ); 
                  data.put(key,value); 
              } 
   
              return true; 
          }catch(Exception E){} 
          return false; 
     } 
  }
 
    
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.