程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> enoeht的Java源碼系列之處理配置文件

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

編輯:JAVA編程入門知識
  我們經常會在程序中用到這樣的配置文件:
  
  Listener = org.kyle.net.svr.sample.SampleListenerImpl
  
  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.getProperty("MainConfigFile","Server.cfg"));
  
  }
  
  public Profile(String configurationFilename)
  
  {
  
  this.m_configurationFilename = configurationFilename;
  
  loadCfg(configurationFilename);
  
  }
  
  public void loadConfig(String configurationFilename)
  
  {
  
  if( configurationFilename == null )
  
  {
  
  System.exit(-1);
  
  }
  
  try {
  
  applicationProps = new Properties();
  
  FileInputStream in = new FileInputStream(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_configurationFilename);
  
  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "8859_1"));
  
  synchronized (applicationProps)
  
  {
  
  Iterator iterator = new TreeSet(applicationProps.keySet()).iterator();
  
  while(iterator.hasNext())
  
  {
  
  String key = (String)iterator.next();
  
  writer.write(key + "=" + applicationProps.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 getString(String key, String Default)
  
  {
  
  String rVal = applicationProps.getProperty(key, Default);
  
  return rVal == null ? rVal : rVal.trim();
  
  }
  
  public String getString(String key)
  
  {
  
  String rVal = applicationProps.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".equalsIgnoreCase(rVal)) return true;
  
  if ("false".equalsIgnoreCase(rVal)) return false;
  
  return Default;
  
  }
  
  public int getInt(String key, int Default)
  
  {
  
  try{
  
  return getInt(key);
  
  }catch(Exception e){
  
  applicationProps.setProperty(key, String.valueOf(Default));
  
  return Default;
  
  }
  
  }
  
  protected int getInt(String key) throws NumberFormatException
  
  {
  
  String rVal = getString(key);
  
  return Integer.parseInt(rVal);
  
  }
  
  public String getConfigurationFilename()
  
  {
  
  return m_configurationFilename;
  
  }
  
  private void loadCfg(String configurationFilename)
  
  {
  
  if( configurationFilename == null )
  
  {
  
  System.out.println("Assigned a null configuration file. Default setting used.");
  
  }
  
  try
  
  {
  
  applicationProps = new Properties();
  
  FileInputStream in = new FileInputStream(configurationFilename);
  
  applicationProps.load(in);
  
  in.close();
  
  }
  
  catch( IOException ioe)
  
  {
  
  System.out.println("Loading configuration from file " + configurationFilename + " failed.");
  
  System.out.println("Default setting 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("Listener", " org.kyle.net.svr.sample.SampleListenerImpl");
  
  }
  
  public InetAddress getServerAddress()
  
  {
  
  try
  
  {
  
  String svrAddr = getString("ServerAddress",null);
  
  if ( svrAddr == null ) return null;
  
  return InetAddress.getByName( 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("StatelessService", true );
  
  }
  
  public String getLogLevel()
  
  {
  
  return getString("LogLevel","CONFIG");
  
  }
  
  public String getLogPath()
  
  {
  
  return getString("LogPath","server.log");
  
  }
  
  }
  
  使用方法:
  String cfgFile ="server.cfg";
  
  GenProfile m_env = new GenProfile( cfgFile );
  
  這樣在程序中就可以使用例如m_env. getServerAddress()等方法取得配置文件的相應內容了。
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved