熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

enoeht的Java源碼系列之處理配置文件

2013-11-23 19:23:44  來源: Java核心技術 

  我們常常會在程序中用到這樣的配置文件
  
  Listener = svrsampleSampleListenerImpl
  
  ServerAddress =
  
  ListeningPort =
  
  ListenerTimeout =
  
  StatelessService = true
  
  LogLevel = ALL
  
  LogPath = serverlog
  
  在這裡提供了一個處理這種配置文件的類的源代碼
  
  package orgkyleutil;
  
  import javaio*;
  
  import javautil*;
  
  //加載配置文件並提供從配置文件中讀取各種類型的值的方法
  
  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(SystemgetProperty(MainConfigFileServercfg));
  
  }
  
  public Profile(String configurationFilename)
  
  {
  
  thism_configurationFilename = configurationFilename;
  
  loadCfg(configurationFilename);
  
  }
  
  public void loadConfig(String configurationFilename)
  
  {
  
  if( configurationFilename == null )
  
  {
  
  Systemexit();
  
  }
  
  try {
  
  applicationProps = new Properties();
  
  FileInputStream in = new FileInputStream(configurationFilename);
  
  applicationPropsload(in);
  
  inclose();
  
  }
  
  catch( IOException ie)
  
  {
  
  Systemexit();
  
  }
  
  }
  
  public void loadConfig()
  
  {
  
  loadConfig( m_configurationFilename );
  
  }
  
  public void saveConfig()
  
  {
  
  try
  
  {
  
  FileOutputStream out = new FileOutputStream(m_configurationFilename);
  
  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out _));
  
  synchronized (applicationProps)
  
  {
  
  Iterator iterator = new TreeSet(applicationPropskeySet()erator();
  
  while(iteratorhasNext())
  
  {
  
  String key = (String)iteratornext();
  
  writerwrite(key + = + applicationPropsgetProperty(key));
  
  writernewLine();
  
  }
  
  }
  
  writerclose();
  
  outclose();
  
  }catch(IOException ie)
  
  {
  
  Systemoutprintln(ietoString());
  
  }
  
  }
  
  public void showConfig()
  
  {
  
  applicationPropslist(Systemout);
  
  }
  
  public Properties getProperty()
  
  {
  
  return applicationProps;
  
  }
  
  String getString(String Section String key String Default)
  
  {
  
  return getString( key Default);
  
  }
  
  public String getString(String key String Default)
  
  {
  
  String rVal = applicationPropsgetProperty(key Default);
  
  return rVal == null ? rVal : rValtrim();
  
  }
  
  public String getString(String key)
  
  {
  
  String rVal = applicationPropsgetProperty(key);
  
  return rVal == null ? rVal : rValtrim();
  
  }
  
  public boolean getBoolean(String key boolean Default)
  
  {
  
  String rVal = getString(key);
  
  //  if (rVal == null) return Default;
  
  if (trueequalsIgnoreCase(rVal)) return true;
  
  if (falseequalsIgnoreCase(rVal)) return false;
  
  return Default;
  
  }
  
  public int getInt(String key int Default)
  
  {
  
  try{
  
  return getInt(key);
  
  }catch(Exception e){
  
  applicationPropssetProperty(key StringvalueOf(Default));
  
  return Default;
  
  }
  
  }
  
  protected int getInt(String key) throws NumberFormatException
  
  {
  
  String rVal = getString(key);
  
  return IntegerparseInt(rVal);
  
  }
  
  public String getConfigurationFilename()
  
  {
  
  return m_configurationFilename;
  
  }
  
  private void loadCfg(String configurationFilename)
  
  {
  
  if( configurationFilename == null )
  
  {
  
  Systemoutprintln(Assigned a null configuration file Default setting used);
  
  }
  
  try
  
  {
  
  applicationProps = new Properties();
  
  FileInputStream in = new FileInputStream(configurationFilename);
  
  applicationPropsload(in);
  
  inclose();
  
  }
  
  catch( IOException ioe)
  
  {
  
  Systemoutprintln(Loading configuration from file + configurationFilename + failed);
  
  Systemoutprintln(Default setting will be used);
  
  }
  
  }
  
  }
  
  package orgkyleutil;
  
  import *;
  
  //調用父類加載配置文件和讀取數據按照配置文件的中的key值讀取其value
  
  public class GenProfile extends Profile
  
  {
  
  public GenProfile()
  
  {
  
  super();
  
  buildCachedCrypt();
  
  }
  
  public GenProfile( String cfgFileName )
  
  {
  
  super( cfgFileName );
  
  buildCachedCrypt();
  
  }
  
  public String getListenerImpl()
  
  {
  
  return getString(Listener svrsampleSampleListenerImpl);
  
  }
  
  public InetAddress getServerAddress()
  
  {
  
  try
  
  {
  
  String svrAddr = getString(ServerAddressnull);
  
  if ( svrAddr == null ) return null;
  
  return InetAddressgetByName( svrAddr );
  
  }
  
  catch( UnknownHostException uhe)
  
  {
  
  (uhe);
  
  }
  
  return null;
  
  }
  
  public int getListenAt()
  
  {
  
  return getInt(ListeningPort );
  
  }
  
  public int getTimeout()
  
  {
  
  return getInt(ListenerTimeout );
  
  }
  
  public boolean statelessService()
  
  {
  
  return getBoolean(StatelessService true );
  
  }
  
  public String getLogLevel()
  
  {
  
  return getString(LogLevelCONFIG);
  
  }
  
  public String getLogPath()
  
  {
  
  return getString(LogPathserverlog);
  
  }
  
  }
  
  使用方法
  String cfgFile =servercfg;
  
  GenProfile m_env = new GenProfile( cfgFile );
  
  這樣在程序中就可以使用例如m_env getServerAddress()等方法取得配置文件的相應內容了

From:http://tw.wingwit.com/Article/program/Java/hx/201311/26782.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.