Java OutputStream equivalent to getClass().getClassLoader().getResourceAsStream()

后端 未结 4 1639
遇见更好的自我
遇见更好的自我 2021-01-04 01:14

I am attempting to store the change made to my application\'s properties. The .properties file is located in resources package, which is different

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 01:39

    When you wrap your app up as a JAR file, your properties file will be one (possibly compressed) file within that JAR, and it would be a bad idea to try to write to your own JAR.

    getResourceAsStream() is meant to open resources for reading, and these can be anywhere on the classpath. You can't write to URLs or inside JARs, you can only write to files, so it doesn't make sense to give you the same API for output.

    Find yourself a directory you're allowed to write into, and write your properties there.

    It may be a good idea to copy your properties from your installation classpath (possibly inside a JAR) directly out to a file if it doesn't yet exist, as a first operation upon application startup. This will give you a properties file you can write to, yet the master copy of this properties file will come from your project deliverable.

提交回复
热议问题