我不确定是否可以更新从classpath加载的属性文件,除非您知道绝对路径?
我想更新属性文件(从classapth加载,我不知道文件的绝对路径)。我无法找到更新从classapth加载的相同属性文件的方法。我写了这段代码。此代码加载文件不会更新并抛出异常。
def updateFile(encrypytedText: String, keyToBeUpdated: String) = {
val keyLocation = PropertyFileReader.readPropertyFile("config.properties")("XXXX")
val p = new Properties()
//keyLocation -> another properties file from classpath
this.getClass.getClassLoader.getResource(keyLocation).getFile
val in = this.getClass.getClassLoader.getResourceAsStream(keyLocation)
p.load(in)
val allProperties = p.propertyNames()
while (allProperties.hasMoreElements) {
val s: String = allProperties.nextElement().toString
p.setProperty(s, p.getProperty(s))
}
p.setProperty(keyToBeUpdated, encrypytedText)
val out = new FileOutputStream(keyLocation)
p.store(out, null)
out.close()
}