有什么方法可以更改类路径中的属性文件吗?
我的类路径中有一个属性文件。我的要求是根据我从参数中获得的一些输入更改此文件中的一些属性。参数的性质决定我是否需要更改属性以及如果需要更改哪些属性。问题是所有类路径条目都是在应用程序启动时加载的,因此从我的应用程序内部进行更改不会产生任何效果。我该如何克服这个问题?
我能想到的一种可能的解决方案是不在类路径中添加此属性文件,而是在修改完成后添加。它可行吗?有什么好的解决办法吗?
I have a properties file that is in my classpath. My requirement is to change some properties in this file based on some inputs I get from arguments. The nature of arguments decide whether I need to change the properties and if yes which properties to change. The problem is that all classpath entries are loaded at the application startup time, so changing from within my application would not have any effect. How do I overcome this problem?
One possible solution I can think of is to not add this properties file in classpath but add after modifications are done. Is it viable? What can be a good solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该文件是否位于您的类路径中并不重要。它是一个文件:如果您覆盖其内容,它就会发生变化。启动时并没有神奇地生成一些内存中的副本。这与加载的类非常不同,类可能需要在运行时进行更改。
遵循正确格式的属性文件可以读入 java.util.Properties 对象。您可以这样做,使用该对象根据需要更改属性,然后将其写回到文件中。检查该类中的
store
和load
方法。请注意,如果您使用采用Output/InputStream
的版本,则编码是硬编码的。如果文件的编码不是 ISO-8859-1,请使用具有适当Writer/Reader
的方法。It doesn't matter whether this file is on your classpath or not. It is a file: if you overwrite its contents, it will have changed. There isn't some in-memory copy that magically gets made at startup. This is very different from classes that are loaded in and which might need change at runtime.
Properties files that adhere to the right format can be read into a java.util.Properties object. You could do that, use the object to alter the properties as needed, then write it back out to the file. Check the
store
andload
methods in that class. Mind that if you use the versions that take anOutput/InputStream
, the encoding is hard-coded. If the file's encoding is anything else than ISO-8859-1, use a method with an appropriateWriter/Reader
.取决于您的应用程序的部署方式。如果您的属性文件位于 jar 内,则您将无法直接更改该属性文件,因为它已打包并压缩在存档中。您可以像其他人提到的那样将这些属性加载到对象中,然后存储/写入到外部位置,可能是基于 URL 的位置。 URL 很方便,因为它可以让您访问几乎任何位置,并且它具有用于加载属性的漂亮的 openStream() 方法。然后,您的应用程序可以在加载时查找新文件,如果无法从新位置读取/加载,则默认为应用程序启动版本。
Depends on how your application is deployed. If your properties files is inside a jar, you won't be able to directly change that properties file since its packaged and zipped up in an archive. You can instead as someone else mentioned load those properties into an object, and then store/write out to an external location, probably a URL based location. URL is convenient because it gets you access to virtually any location and it has that nifty openStream() method for loading properties. Your application could then look for the new file on load, and default to the application startup version if it fails to read/load from the new location.
这是示例代码:
Here is a sample code:
您需要首先从属性文件中删除该属性,然后重新定义它。他们没有办法直接修改属性文件。
下面是一个例子:
You need to first remove that property from the property file and then re-define it. Their is no way to directly modify the properties file.
Below is an example: