Java相当于app.config?
Java 中是否有相当于 .NET 的 App.Config 的工具?
如果没有,是否有标准方法来保留应用程序设置,以便在应用程序分发后可以更改它们?
Is there a Java equivalent to .NET's App.Config?
If not is there a standard way to keep you application settings, so that they can be changed after an app has been distributed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 WebApps,web.xml 可用于存储应用程序设置。
除此之外,您可以使用 属性类来读取和写入属性文件。
您可能还想查看首选项 类,用于读写系统和用户首选项。 它是一个抽象类,但您可以使用
userNodeForPackage(ClassName.class)
和systemNodeForPackage(ClassName.class)
获取适当的对象。For WebApps, web.xml can be used to store application settings.
Other than that, you can use the Properties class to read and write properties files.
You may also want to look at the Preferences class, which is used to read and write system and user preferences. It's an abstract class, but you can get appropriate objects using the
userNodeForPackage(ClassName.class)
andsystemNodeForPackage(ClassName.class)
.将 @Powerlord 使用
Properties
类的建议 (+1) 放入示例代码中:然后,在容器的根目录(例如 jar 文件的顶部)放置一个文件
Configuration.cnf
包含以下内容:这感觉并不完美(我有兴趣听到改进),但足以满足我当前的需求。
To put @Powerlord's suggestion (+1) of using the
Properties
class into example code:Then, at the root of the container (e.g. top of a jar file) place a file
Configuration.cnf
with the following content:This feel not perfect (I'd be interested to hear improvements) but good enough for my current needs.
简单的方法是简单地拥有一个属性文件,例如 myapp.properties,其中包含所有设置。这不是一种非常高级的设置方法,但它已经足够了,或者您可以拥有自己的基于 XML 的设置,或者从数据库等
The simple way is to simply have a properties file, e.g., myapp.properties, with all your settings in. It's not a very advanced way to do settings but it suffices, or you can have your own XML based setup, or get them from a database, etc.