我们常常会在程序中用到这样的配置文件:
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()等方法取得配置文件的相应内容了。
闽公网安备 35060202000074号