讀取配置工具類
阿新 • • 發佈:2018-12-02
import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * @author Sam * @Description: 讀取配置工具類 * @date 2018/5/23 */ public class PropertiesUtils { private static Properties props; static { loadProps(); } synchronized static private void loadProps() { props = new Properties(); InputStream in = null; try { // 要載入的屬性檔案 in = PropertiesUtils.class.getResourceAsStream("/config.properties"); props.load(in); } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != in) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static String getProperty(String key) { if (null == props) { loadProps(); } return props.getProperty(key); } public static String getProperty(String key, String defaultValue) { if (null == props) { loadProps(); } return props.getProperty(key, defaultValue); } }