-
編寫
.properties
文件- 文件編寫以 [
key
=value
] 格式 -
=
符號是允許有空格的, 程序讀取時會去除=
左右空格 - 數據以換行來標識結束
- 文件編寫以 [
-
定義類獲取
.peroperties
文件中的值- 獲取本類
Class
對象 - 通過
Class
對象獲取ClassLoader
類加載器 - 通過類加載器的
getResourceAsStream()
方法獲取字節流 - 定義
Properties
集合, 調用集合中的load(inputStream)
來讀取文件 - 通過
Properties
集合中的getProperty("key")
來獲取對於的值
- 獲取本類
編寫
.properties
文件
key=value
mark=markValue
info=infoVlaue
定義類獲取
.peroperties
文件中的值
public static void main(String[] args) throws IOException {
InputStream inputStream = PropertiesDemo.class.getClassLoader().getResourceAsStream("info.properties");
Properties properties = new Properties();
properties.load(inputStream);
System.out.println("key:[" + properties.getProperty("key") + "]");
System.out.println("mark:[" + properties.getProperty("mark") + "]");
System.out.println("info:[" + properties.getProperty("info") + "]");
}
那麼執行程序將輸出結果爲:
key:[value]
mark:[markValue]
info:[infoVlaue]