分类

  • 软件天地

  • enoeht的Java源码系列之处理配置文件‖
      Listener = org.kyle.
    net.svr.sample.SampleListene
    rImpl

      ServerAddress = 127.0.0.1                            

      ListeningPort = 80                                          

      ListenerTimeout = 120                                    

      StatelessService = true                                

      LogLevel = ALL                                                  

      LogPath = server.log                                      

      在这里提供了一个处理这种配置文件的类的源代码。                                

      package org.kyle.util;                                  

      import java.io.*;                                            

      import java.util.*;                                        

      //加载配置文件,并提供从配置文件中读取各种类型的值的方法                    

      public class Profile                                      

      {                                                                            

      protected Properties applicationProps;  

      protected String m_
    configurationFilename = null
    ;

      private boolean m_debug = false;              

      public Profile( boolean debug)                  

      {                                                                            

      this();                                                                

      m_debug = debug;                                              

      }                                                                            

      public Profile()                                              

      {                                                                            

      this(System.getPrope
    rty("MainConfigFile","Server
    .cfg"));

      }                                                                            

      public Profile(String configurat
    ionFilename)

      {                                                                            

      this.m_configurationFilename = c
    onfigurationFilename;

      loadCfg(configurationFilename);                

      }                                                                            

      public void loadConf
    ig(String configurationFilen
    ame)

      {                                                                            

      if( configurationFilename == null )        

      {                                                                            

      System.exit(-1);                                              

      }                                                                            

      try {                                                                    

      applicationProps = new Properties();      

      FileInputStream in = new FileInp
    utStream(configurationFilename);

      applicationProps.load(in);                          

      in.close();                                                        

      }                                                                            

      catch( IOException ie)                                  

      {                                                                            

      System.exit(-1);                                              

      }                                                                            

      }                                                                            

      public void loadConfig()                              

      {                                                                            

      loadConfig( m_configurationFilename );  

      }                                                                            

      public void saveConfig()                              

      {                                                                            

      try                                                                        

      {                                                                            

      FileOutputStream out
    = new FileOutputStream(m_co
    nfigurationFilename);

      BufferedWriter write
    "8859_1"));
    r = new BufferedWriter(new O

    utputStreamWriter(out,


      synchronized (applicationProps)                

      {                                                                            

      Iterator iterator =
    new TreeSet(applicationProps
    .keySet()).iterator();

      while(iterator.hasNext())                            

      {                                                                            

      String key = (String)iterator.next();    

      writer.write(key + "=" + applica
    tionProps.getProperty(key));

      writer.newLine();                                            

      }                                                                            

      }                                                                            

      writer.close();                                                

      out.close();                                                      

      }catch(IOException ie)                                  

      {                                                                            

      System.out.println(ie.toString());          

      }                                                                            

      }                                                                            

      public void showConfig()                              

      {                                                                            

      applicationProps.list(System.out);          

      }                                                                            

      public Properties getProperty()                

      {                                                                            

      return applicationProps;                              

      }                                                                            

      String getString(String Section,
    String key, String Default)

      {                                                                            

      return getString( key, Default);              

      }                                                                            

      public String getStr
    ing(String key, String Defau
    lt)

      {                                                                            

      String rVal = applic
    ationProps.getProperty(key,
    Default);

      return rVal == null ? rVal : rV
    al.trim();

      }                                                                            

      public String getString(String key)        

      {                                                                            


      String rVal = applic
    ationProps.getProperty(key);


      return rVal == null
    ? rVal : rVal.trim();

      }                                                                            

      public boolean getBoolean(String
    key, boolean Default)

      {                                                                            

      String rVal = getString(key);                    

      //  if (rVal == null) return Default;    


      if ("true".equalsIgn
    oreCase(rVal)) return true;

      if ("false".equalsIgnoreCase(rVa
    l)) return false;

      return Default;                                                

      }                                                                            


      public int getInt(St
    ring key, int Default)

      {                                                                            

      try{                                                                      

      return getInt(key);                                        

      }catch(Exception e){                                      

      applicationProps.set
    Property(key, String.valueOf
    (Default));

     eturn Default;                                                

      }                                                                            

      }                                                                            

      protected int getInt(String key)
    throws NumberFormatException

      {                                                                            

      String rVal = getString(key);                    

      return Integer.parseInt(rVal);                  

      }                                                                            


      public String getCon
    figurationFilename()

      {                                                                            

      return m_configurationFilename;                

      }                                                                            

      private void loadCfg(String conf
    igurationFilename)

      {                                                                            

      if( configurationFilename == null )        

      {                                                                            

      System.out.println("Assigned a n
    used.");
    ull configuration file. Default setting


      }                                                                            

      try                                                                        

      {                                                                            

      applicationProps = new Properties();      

      FileInputStream in =
    new FileInputStream(configu
    rationFilename);

      applicationProps.load(in);                          

      in.close();                                                        

      }                                                                            

      catch( IOException ioe)                                

      {                                                                            

      System.out.println("
    + " failed.");
    Loading configuration from f

    ile " + configurationFilename


      System.out.println("Default sett
    ing will be used.");

      }                                                                            

      }                                                                            

      }                                                                            

      package org.kyle.util;                                  

      import java.net.*;                                          

      //调用父类加载配置文件和读取数据
    ,按照配置文件的中的key值读取其value。

      public class GenProfile extends Profile

      {                                                                            

      public GenProfile()                                        

      {                                                                            

      super();                                                              

      buildCachedCrypt();                                        

      }                                                                            

      public GenProfile( String cfgFileName )

      {                                                                            

      super( cfgFileName );                                    

      buildCachedCrypt();                                        

      }                                                                            

      public String getListenerImpl()                

      {                                                                            

      return getString("Li
    stener", " org.kyle.net.svr.
    sample.SampleListenerImpl");

      }                                                                            

      public InetAddress getServerAddress()    

      {                                                                            

      try                                                                        

      {                                                                            

      String svrAddr = get
    String("ServerAddress",null)
    ;

      if ( svrAddr == null ) return null;        


      return InetAddress.g
    etByName( svrAddr );

      }                                                                            

      catch( UnknownHostException uhe)              

      {                                                                            

      Debug.info(uhe);                                              

      }                                                                            

      return null;                                                      

      }                                                                            

      public int getListenAt()                              

      {                                                                            

      return getInt("ListeningPort", 80);        

      }                                                                            

      public int getTimeout()                                

      {                                                                            

      return getInt("ListenerTimeout", 120);  

      }                                                                            

      public boolean statelessService()            

      {                                                                            


      return getBoolean("S
    tatelessService", true );

      }                                                                            

      public String getLogLevel()                        

      {                                                                            

      return getString("LogLevel","CONFIG");  

      }                                                                            

      public String getLogPath()                          

      {                                                                            


      return getString("Lo
    gPath","server.log");

      }                                                                            

      }                                                                            

      使用方法:

      String cfgFile ="server.cfg";                    

      GenProfile m_env = new GenProfil
    e( cfgFile );

      这样在程序中就可以使
    容了。
    用例如m_env. getServerAddres

    s()等方法取得配置文件的相应内



    上一页 下一页




    map