根據key從Properties檔案中載入指定的value
阿新 • • 發佈:2019-02-12
//單例模式實現讀取***.properties檔案的內容
public class OVLoadProperties {
// 宣告一個自己的例項
private static OVLoadProperties instance = new OVLoadProperties();
final static String fileName = "/messages_zh_CN.properties";
// 返回該例項
public static synchronized OVLoadProperties getInstance() {
return instance;
}
// 獲取key所對應的值
public String getProperties(String key) {
Properties p = new Properties();
InputStream is = null;
try {
// ***.properties檔案放在src目錄的下邊
is = OVLoadProperties.class.getResourceAsStream(fileName);
if (is == null)
is = new FileInputStream(fileName);
p.load(is );
} catch (Exception e) {
System.out.println("載入檔案出錯啦!" + e.getMessage());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out .println(e.getMessage());
}
}
}
return p.getProperty(key);
}
}