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);
}
}
//config.properties在src下面
//新建测试类
package weibo4j.mytest.util;
import weibo4j.util.WeiboConfig;
public class TestWeiboConfig {
@SuppressWarnings("static-access")
public static void main(String[] args) {
WeiboConfig wc = new WeiboConfig();
//APPKEY
String client_ID = wc.getValue("client_ID");
//APPSERCRE
String client_SERCRE = wc.getValue("client_SERCRE");
//CALLBAKURL
String redirect_URI = wc.getValue("redirect_URI");
String baseURL = wc.getValue("baseURL");
String accessTokenURL = wc.getValue("accessTokenURL");
String authorizeURL = wc.getValue("authorizeURL");
System.out.println("client_ID "+client_ID);
System.out.println("client_SERCRE "+client_SERCRE);
System.out.println("redirect_URI "+redirect_URI);
System.out.println("baseURL "+baseURL);
System.out.println("accessTokenURL "+accessTokenURL);
System.out.println("authorizeURL "+authorizeURL);
wc.updateProperties("authorizeURL", "123123123123");
authorizeURL = wc.getValue("authorizeURL");
System.out.println("authorizeURL "+authorizeURL);
}
}
//这样就可以读取配置文件了