日期:2014-05-17  浏览次数:20721 次

java获取配置文件后的返回值
java获取配置文件后的返回值问题,systemConfi该如何赋值
Java code
public class SystemConfigUtil
{
    
    public static SystemConfig getSystemConfig()
    {
        
        InputStream inputStream = SystemConfigUtil.class.getClassLoader().getResourceAsStream("config.properties");
        
        Properties p = new Properties();
        
      try{
          p.load(inputStream);
      }catch(IOException e){
          e.printStackTrace();
      }
     // System.out.println("FilePath"+p.getProperty("FilePath")+",ImagePath"+p.getProperty("ImagePath")+",PortraitPath"+p.getProperty("PortraitPath"));
        
       
       return systemConfig;//该如何赋值....初学不是很懂
    }
   
}


------解决方案--------------------
SystemConfig systemConfig = new SystemConfig();
systemConfig.setXxx(p.get("Xxx"));
------解决方案--------------------
Java code

import java.io.IOException;
import java.util.Properties;

import org.omg.CORBA.portable.InputStream;

/**
 * 我猜测这是一个配置文件类
 * @author qian.xu
 *
 */
public class SystemConfigUtil
{
    private static Properties p = null;
    private static SystemConfigUtil systemConfigUtil  = new SystemConfigUtil();
    
    //这里用到了单例模式,所以私有化构造方法
    private SystemConfigUtil(){
    }
    
    /**
     * 对外公开的方法,是SystemConfigUtil获得配置文件
     * @return systemConfigUtil
     */
    public static SystemConfigUtil getSystemConfig()
    {
        InputStream inputStream = (InputStream) SystemConfigUtil.class.getClassLoader()
                                                   .getResourceAsStream("config.properties");
              
      try{
          if(systemConfigUtil.p==null){
              systemConfigUtil.p.load(inputStream);//如果systemConfigUtil中p没有东西给他加上东西
                                                   //否则返回
          }
      }catch(IOException e){
          e.printStackTrace();
      }
     // System.out.println("FilePath"+p.getProperty("FilePath")+",ImagePath"+p.getProperty("ImagePath")+",PortraitPath"+p.getProperty("PortraitPath"));
       
       return systemConfigUtil;//
    }
   
}