共計 699 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Java 中,可以通過 Properties 類來讀取和寫入屬性文件。下面是 Properties 類的使用示例:
- 創建 Properties 對象并加載屬性文件:
Properties properties = new Properties();
try {FileInputStream fileInputStream = new FileInputStream("config.properties");
properties.load(fileInputStream);
fileInputStream.close();} catch (IOException e) {e.printStackTrace();
}
- 讀取屬性值:
String value = properties.getProperty("key");
System.out.println(value);
- 設置屬性值:
properties.setProperty("key", "value");
- 保存屬性文件:
try {FileOutputStream fileOutputStream = new FileOutputStream("config.properties");
properties.store(fileOutputStream, "Config Properties");
fileOutputStream.close();} catch (IOException e) {e.printStackTrace();
}
以上就是使用 Properties 類讀取和寫入屬性文件的基本步驟。需要注意的是,屬性文件的格式為鍵值對,如 ”key=value”,每個屬性占一行。
丸趣 TV 網 – 提供最優質的資源集合!
正文完
發表至: Java
2023-12-20