我正在为生产监控编写独立的Java应用程序.一旦它开始运行,api就配置为在.properties文件中设置的默认值.在运行状态下,可以更改api的配置,并相应地更新.properties文件.有没有办法实现这个目标?还是有其他方法来实现这个?
提前致谢
Pas*_*ent 10
您可以使用基于java.util.Properties类的非常简单的方法,该类确实有一个加载和存储方法,您可以将它们与FileInputStream和FileOutputStream结合使用:
但实际上,我建议使用现有的配置库,如Commons Configuration(以及其他配置库).检查属性Howto以了解如何使用其API加载,保存和自动重新加载属性文件.
我完全同意 Apache Commons Configuration API 确实是不错的选择。
此示例在运行时更新属性
File propertiesFile = new File(getClass().getClassLoader().getResource(fileName).getFile());
PropertiesConfiguration config = new PropertiesConfiguration(propertiesFile);
config.setProperty("hibernate.show_sql", "true");
config.save();
Run Code Online (Sandbox Code Playgroud)
希望这有帮助!