主要是用到了靜態塊,和反射機制獲得類的路徑。 java
package com.weibo.weibo4j.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; @SuppressWarnings("static-access") public class WeiboConfig { private static String filePath = WeiboConfig.class.getResource("/").getPath() + "sinaywconfig.properties"; public WeiboConfig() { } private static Properties props = new Properties(); static { try { String filePaths = new URLEncodeUtils().decodeURL(filePath); System.out.println(filePaths); props.load(new FileInputStream(filePaths)); } 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); } }