当前位置: 代码迷 >> Web前端 >> 读取WEB-INF/classes目录上文件,及读取其key-value
  详细解决方案

读取WEB-INF/classes目录上文件,及读取其key-value

热度:402   发布时间:2012-12-17 09:31:40.0
读取WEB-INF/classes目录下文件,及读取其key-value
package com.ui.version.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Config {

	private static Logger log = Logger.getLogger(Config.class.getName());
	
	private static final String fileName = "xiu-ui.properties";
	private static final String fileName2 = "com/ui/version/resources/xiu-ui.properties";
	
	
	private static Properties properties = new Properties();;
	
	static{
		boolean ready = false;
		InputStream is = Config.class.getClassLoader().getResourceAsStream(fileName);
		if(is != null){
			try {
				properties.load(is);
				ready = true;
				log.log(Level.INFO, "加载classpath:"+fileName+"成功!");
			} catch (IOException e) {
				log.log(Level.WARNING, "加载classpath:"+fileName+"失败", e);
			}
		}
		if(!ready){
			is = Config.class.getClassLoader().getResourceAsStream(fileName2);
			if(is != null){
				try {
					properties.load(is);
					ready = true;
					log.log(Level.INFO, "加载classpath:"+fileName2+"成功!");
				} catch (IOException e) {
					log.log(Level.WARNING, "加载classpath:"+fileName2+"失败", e);
				}
			}
		}
		if(!ready){
			log.log(Level.SEVERE, "未能找到资源,请将"+fileName+"文件放到classes目录下");
		}
	}
	
	public static String getConfigValue(String key){
		return properties.getProperty(key,null);
	}
	
	public static Properties getProperties(){
		return properties;
	}
	
}

  相关解决方案