`

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

    博客分类:
  • java
 
阅读更多
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;
	}
	
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics