gpt4 book ai didi

java - 在java中更新具有键值形式值的文件

转载 作者:行者123 更新时间:2023-11-30 07:09:50 25 4
gpt4 key购买 nike

我需要更新包含键值形式的值的文件(仅一个特定行)。

app.num_hosts=4
app.resourceid=broker0

我打算读取 map 中的所有文件,然后修改特定字段并重写文件。这是更新文件的好方法吗?我可以使用哪个 API 将 map 写入文件?

通过搜索现有问题,我无法找到一种方法来只更新单行而不重写整个文件。

最佳答案

听起来您本质上是想使用 java.util.properties 库。

public static void main(String[] args) {

Properties prop = new Properties();
OutputStream output = null;

try {
//load the file into properties object
FileInputStream input = new FileInputStream("config.properties");
prop.load(input);
input.close();

// set the properties value
output = new FileOutputStream("config.properties");
prop.setProperty("app.num_hosts", "4");
prop.setProperty("app.resourceid", "broker0");
prop.store(output, null);


} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
}

blog post进一步概述,但您要做的是首先读取属性文件,进行更新,然后将其写回。

关于java - 在java中更新具有键值形式值的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22650669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com