向properties文件中写入信息(针对获取properties文件失败的总结)

时间:2023-03-09 09:15:36
向properties文件中写入信息(针对获取properties文件失败的总结)

前段时间项目需要将某个属性动态的写入项目发布路径下的properties文件中;但是实际发布时发现找不到maven项目resource路径下的project.properties文件,调试多次代码如下:

/**
* 写入properties信息
*
* @param key 名称
* @param value 值
*/
public static void modifyConfig(String key, String value) {
try {
// 从输入流中读取属性列表(键和元素对)
Properties prop = getProperties();
prop.setProperty(key, value);
//Global.class为当前类名,project.properties为路径
String path = Global.class.getResource("/project.properties").getPath();
FileOutputStream outputFile = new FileOutputStream(path);
prop.store(outputFile, "modify");
outputFile.close();
outputFile.flush();
} catch (Exception e) {
e.printStackTrace();
}
}