enoeht的Java源码系列(3)--处理配置文件
2004-06-23 12:10
736 查看
我们常常会在程序中用到这样的配置文件: 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()等方法取得配置文件的相应内容了。
相关文章推荐
- enoeht的Java源码系列(4)--继续处理配置文件
- enoeht的Java源码系列(6)--调试信息与日志文件
- java应用程序中处理配置文件和图片路径在打包后路径不对的问题
- Java乔晓松-SSH框架整合配置文件的配置以及事务处理
- 从0开始写JavaWeb框架系列(1)从0开始写SamrtFrameWork:读取配置文件
- java学习笔记7 - Spring mvc 统一异常处理和静态文件的配置
- enoeht的Java源码系列(2)--综合工厂
- [java热部署系列 1] 动态加载配置文件的正确姿势
- Error:java: 服务配置文件不正确, 或构造处理程序对象javax.annotation.processing.Processor: Provider org.antlr.v4.runtime
- Maven和Mybaties结合,Maven不处理java文件夹下面的配置文件
- Java开发小技巧(四):配置文件敏感信息处理
- java文件处理源码
- PHP源码安装后找不到配置文件的问题处理
- 对比JAVA学习PHP系列:应用:php的文件系统处理
- JAVA技术:XML配置文件的读取处理
- 【脚本语言系列】关于Python结构化文本文件处理配置文件.ini,你需要知道的事
- 轻量级ORM开发系列:缓存类信息以及配置文件的处理
- struts2源码系列(4)--配置文件解析
- enoeht的Java源码系列(5)--字符串加解密
- springMVC系列源码之配置文件深入——11