梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
package org.siyn.util;

import java.io.InputStream;
import java.util.Properties;

/**
 * <p>
 * 本类用提供一个线程同步的方法,读取资源文件中的配置信息
 * </p>
 * 
 * 
@author siyn
 * @date 2008-7-10
 
*/
public class PropertiesReader
{
    
private String     file;
    
    
private Properties properties;
    
    
/**
     * 构造 PropertysReader
     * 
@param {String} path 相对于classes的文件路径
     
*/
    
public PropertiesReader(String path)
    {
        
this.file = path;
        
this.properties = new Properties();
    }
    
    
/**
     * <p>
     * 本方法根据资源名获取资源内容
     * <p>
     * 
     * 
@param {String} key 资源文件内key
     * 
@param {Stirng} defaultValue 默认值
     * 
     * @reaurn String key对应的资源内容
     
*/
    
public synchronized String getProperty(String key, String defaultValue)
    {
        
try
        {
            InputStream in 
= this.getClass().getClassLoader()
                    .getResourceAsStream(
this.file);
            
            properties.load(in);
            
        }
        
catch (Exception ex1)
        {
            System.out.println(
"没有找到资源文件:" + this.file);
        }
        
return properties.getProperty(key, defaultValue);
    }
    
    
/**
     * <p>
     * 本方法根据资源名获取资源内容
     * <p>
     * 
     * 
@param {String} key 资源文件内key
     * 
@param {Stirng} defaultValue 默认值
     * 
@param {boolean} isnull 如果配置文件value为空,是否使用默认值
     * 
     * @reaurn String key对应的资源内容
     
*/
    
public synchronized String getProperty(String key, String defaultValue,boolean isnull)
    {
        String value 
= null;
        value 
= getProperty(key,defaultValue);
        
if(isnull && value == null && "".equals(value.trim()) )
            value 
= defaultValue;
        
return value;
    }
    
    
public static void main(String[] args)
    {
        PropertiesReader preader 
= new PropertiesReader("log4j.properties");
        String rootLogger 
= preader.getProperty("aaa""defaul");
        System.out.println(rootLogger);
    }
}
posted on 2008-07-21 14:48 HUIKK 阅读(257) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: