Java/Scala从类路径更新属性文件

Java/Scala从类路径更新属性文件,java,file,scala,properties,Java,File,Scala,Properties,我不确定是否可以更新从类路径加载的属性文件,除非您知道绝对路径 我想更新一个属性文件(从classapth加载,我不知道文件的绝对路径)。我找不到更新从classapth加载的相同属性文件的方法。我写了这段代码。此代码加载不更新的文件并引发异常 def updateFile(encrypytedText: String, keyToBeUpdated: String) = { val keyLocation = PropertyFileReader.readPropertyFile("c

我不确定是否可以更新从类路径加载的属性文件,除非您知道绝对路径

我想更新一个属性文件(从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()
}

这个文件是iside JAR还是仅在类路径中?它是可写的吗?你得到了什么样的异常?文件在jar之外,但在类路径中。文件是可写的。我没有任何例外。我可以读取该文件,但在写入时,它不会更新相同的文件,而是在project home中创建一个新的文件。首先,如果您可以获取
java.io.file
,则可以选中
getAbsolutePath
。若你们有它,你们可以删除旧文件并创建另一个(它应该比从一开始就清理文件和写文件要快)。你们在描述中提到你们得到了例外,现在你们在最新的评论中声称并没有这样的例外。如果你在这里得到异常粘贴,它是哪一个,它的消息。那
属性FileReader
(库)是什么?