我們常常會在程序中用到這樣的配置文件
Listener =
ServerAddress =
ListeningPort =
ListenerTimeout =
StatelessService = true
LogLevel = ALL
LogPath = server
在這裡提供了一個處理這種配置文件的類的源代碼
package org
import java
import java
//加載配置文件
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
}
public Profile(String configurationFilename)
{
this
loadCfg(configurationFilename);
}
public void loadConfig(String configurationFilename)
{
if( configurationFilename == null )
{
System
}
try {
applicationProps = new Properties();
FileInputStream in = new FileInputStream(configurationFilename);
applicationProps
in
}
catch( IOException ie)
{
System
}
}
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(applicationProps
while(iterator
{
String key = (String)iterator
writer
writer
}
}
writer
out
}catch(IOException ie)
{
System
}
}
public void showConfig()
{
applicationProps
}
public Properties getProperty()
{
return applicationProps;
}
String getString(String Section
{
return getString( key
}
public String getString(String key
{
String rVal = applicationProps
return rVal == null ? rVal : rVal
}
public String getString(String key)
{
String rVal = applicationProps
return rVal == null ? rVal : rVal
}
public boolean getBoolean(String key
{
String rVal = getString(key);
// if (rVal == null) return Default;
if (
if (
return Default;
}
public int getInt(String key
{
try{
return getInt(key);
}catch(Exception e){
applicationProps
return Default;
}
}
protected int getInt(String key) throws NumberFormatException
{
String rVal = getString(key);
return Integer
}
public String getConfigurationFilename()
{
return m_configurationFilename;
}
private void loadCfg(String configurationFilename)
{
if( configurationFilename == null )
{
System
}
try
{
applicationProps = new Properties();
FileInputStream in = new FileInputStream(configurationFilename);
applicationProps
in
}
catch( IOException ioe)
{
System
System
}
}
}
package org
import
//調用父類加載配置文件和讀取數據
public class GenProfile extends Profile
{
public GenProfile()
{
super();
buildCachedCrypt();
}
public GenProfile( String cfgFileName )
{
super( cfgFileName );
buildCachedCrypt();
}
public String getListenerImpl()
{
return getString(
}
public InetAddress getServerAddress()
{
try
{
String svrAddr = getString(
if ( svrAddr == null ) return null;
return InetAddress
}
catch( UnknownHostException uhe)
{
(uhe);
}
return null;
}
public int getListenAt()
{
return getInt(
}
public int getTimeout()
{
return getInt(
}
public boolean statelessService()
{
return getBoolean(
}
public String getLogLevel()
{
return getString(
}
public String getLogPath()
{
return getString(
}
}
使用方法
String cfgFile =
GenProfile m_env = new GenProfile( cfgFile );
這樣在程序中就可以使用例如m_env
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26782.html