java

位置:IT落伍者 >> java >> 浏览文章

enoeht的Java源码系列之处理配置文件


发布日期:2020年11月09日
 
enoeht的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 Stringm_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);

returnrVal == null ? rVal : rValtrim();

}

public String getString(String key)

{

String rVal = applicationPropsgetProperty(key);

returnrVal == 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 booleanstatelessService()

{

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()等方法取得配置文件的相应内容了

上一篇:JAVA对象转为Java String的几种常用方法

下一篇:一个用JAVA开发的会话密钥程序,可能对你有所帮助