在Java中,可以使用java.util.Properties
类来读取和操作属性配置文件。下面是一些常见的使用方法:
- 创建
Properties
对象并加载配置文件:
Properties props = new Properties(); try(InputStream inputStream = new FileInputStream("config.properties")) { props.load(inputStream); }
- 读取属性值:
String value = https://www.yisu.com/ask/props.getProperty("key");
- 设置属性值:
props.setProperty("key", "value");
- 保存修改后的配置文件:
try(OutputStream outputStream = new FileOutputStream("config.properties")) { props.store(outputStream, "Configurations"); }
上述代码中,config.properties
是配置文件的名称,可以根据实际情况进行替换。配置文件的内容如下所示:
key1=value1 key2=value2
你可以根据需要在配置文件中添加或修改属性的值。