有什么办法可以更改我的类路径中的属性文件吗? [英] Is there any way to change a properties file that is in my classpath?

查看:99
本文介绍了有什么办法可以更改我的类路径中的属性文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类路径中有一个属性文件.我的要求是根据我从参数中获得的一些输入来更改此文件中的某些属性.参数的性质决定了是否需要更改属性,如果需要,则更改哪些属性.问题在于,所有类路径条目都在应用程序启动时加载,因此从我的应用程序内部进行更改不会产生任何影响.我该如何克服这个问题?

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?

我能想到的一种可能的解决方案是不在classpath中添加此属性文件,而是在完成修改后添加.可行吗?有什么好的解决方案?

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?

推荐答案

取决于应用程序的部署方式.如果您的属性文件位于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.

这篇关于有什么办法可以更改我的类路径中的属性文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆