问题描述
有没有办法修改作为资源存储在Jar文件中的属性文件中的属性值?
Is there a way to modify the property values in a Properties file that is stored in a Jar file as a Resource?
这是我正在尝试的方案处理。我有一个属性文件存储在我的Jar文件中作为资源。有一些特定于系统的属性,例如路径。我希望能够为我希望运行Jar文件的系统更改此设置。
This is the scenario that I am trying to handle. I have a properties file stored as a resource in my Jar file. There are some system specific properties, for example, paths. I want to be able to change this for the system that I want the Jar file to run on.
最好的解决方案是调用Jar文件内部的类/方法,它将修改Jar中包含的资源文件。另一种解决方案是在Jar文件上运行一个实用程序,它将修改属性文件并重新生成所有内容。这将是我在Jar文件上运行的一次性设置过程。
The best solution would be to call a class/method that is internal to the Jar file that will modify the resource file that is contained in the Jar. Another solution would be to run a utility program on the Jar file that will modify the property file and re-Jar everything. This will be a one-time setup process that I run on the Jar file.
推荐答案
使用首选项API存储更改:
Use the preferences API to store changes: http://java.sun.com/j2se/1.5.0/docs/api/java/util/prefs/package-frame.html
重新阅读后编辑问题:您是否希望将JAR发送给潜在的许多人并在安装时进行自定义?如果是的话,那么prefs是可行的方法。
Edit after re-reading the question: are you in a situation where you want to send out the JAR to potentially many people and have it customized on install? If yes, then prefs is the way to go.
相反,如果你想为相对较少的部署进行自定义,那么可能更好在构建时进行自定义。
If, instead, you're looking to customize for a relatively small number of deployments, then it's probably better to do the customization at build-time.
这篇关于写入包含在Jar文件中作为资源的Java Properties文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!