共計 532 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Java 中,可以使用 java.util.Properties
類來讀取和操作屬性配置文件。下面是一些常見的使用方法:
- 創建
Properties
對象并加載配置文件:
Properties props = new Properties();
try(InputStream inputStream = new FileInputStream("config.properties")) {props.load(inputStream);
}
- 讀取屬性值:
String value = props.getProperty("key");
- 設置屬性值:
props.setProperty("key", "value");
- 保存修改后的配置文件:
try(OutputStream outputStream = new FileOutputStream("config.properties")) {props.store(outputStream, "Configurations");
}
上述代碼中,config.properties
是配置文件的名稱,可以根據實際情況進行替換。配置文件的內容如下所示:
key1=value1
key2=value2
你可以根據需要在配置文件中添加或修改屬性的值。
丸趣 TV 網 – 提供最優質的資源集合!
正文完