java 操作Properties
package weibo4j.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class WeiboConfig {
public WeiboConfig(){}
private static Properties props = new Properties();
static{
try {
props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getValue(String key){
return props.getProperty(key);
}
public static void updateProperties(String key,String value) {
props.setProperty(key, value);
}
}
使用
public String authorize(String response_type) throws WeiboException {
return WeiboConfig.getValue("authorizeURL").trim() + "?client_id="
+ WeiboConfig.getValue("client_ID").trim() + "&redirect_uri="
+ WeiboConfig.getValue("redirect_URI").trim()
+ "&response_type=" + response_type;
}
位置
Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取,path不能以’/'开头,最终是由ClassLoader获取资源。