直接贴代码吧
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
public class PropertyUtils {
public static String getValueByKey(Class clazz, String propertyFilename, String key) {
Properties prop = new Properties();
try{
//读取属性文件a.properties
InputStream in = clazz.getClassLoader().getResourceAsStream(propertyFilename);
prop.load(in); ///加载属性列表
return prop.getProperty(key);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
用法,如:
Class clazz = this.getClass();
String domainName = PropertyUtils.getValueByKey(clazz, "xxx.properties", "domainName");