java获取properties资源文件内容

package com.jsajax.utilities;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PropertiesUtility {

	public static Map<String,String> properties ;
	public static String getConfValue(String confKey){
		
		if(properties==null || properties.size()==0){
			properties = new HashMap<String,String>();
			retrieveFromProFiles(properties);
			retrieveFromDB(properties);
		}
		
		return properties.get(confKey);
	}
	
	public static void retrieveFromProFiles(Map<String,String> proMap){
		InputStream is = null;
		Properties ps = new Properties();
		try {
			/**no "/" relative path of current class,"/"absolute path*/
//			is = PropertiesUtility.class.getResourceAsStream("/configuration.properties");
			/**classpath*/
			is = PropertiesUtility.class.getClassLoader().getResourceAsStream("configuration.properties");
			/**project path*/
//			is = new FileInputStream(new File("src/main/resources/configuration.properties"));
			
			ps.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Set<Object> confKeys = ps.keySet();
		if(confKeys!=null && confKeys.size()>0){
			String keyStr;
			for(Object key : confKeys){
				keyStr = (String)key;
				proMap.put(keyStr, ps.getProperty(keyStr));
			}
		}
	}
	public static void retrieveFromDB(Map<String,String> proMap){
		//TODO...
	}
	
	public static void main(String[] args) {
		System.out.println(PropertiesUtility.getConfValue("jslearn.upload.path"));
	}
}

 

你可能感兴趣的:(java properties)