java更新属性文件运行时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1659642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:47:15  来源:igfitidea点击:

java update properties file run time

java

提问by Bharani

i am writing standalone java app for production monitoring. once it starts running the api is configured for default values which is set in .properties file. in running state the api's configuration can be changed and the .properties file should be updated accordingly. is there a way to achieve this ? or are there any other approaches to implement this ?

我正在编写用于生产监控的独立 Java 应用程序。一旦它开始运行,api 就会被配置为在 .properties 文件中设置的默认值。在运行状态下,可以更改 api 的配置,并相应地更新 .properties 文件。有没有办法实现这一目标?或者有没有其他方法来实现这一点?

Thanks in advance

提前致谢

采纳答案by Steven Schlansker

The Java Properties class (api here) specifies "load" and "store" methods which should do exactly that. Use FileInputStream and FileOutputStream to specify the file to save it into.

Java Properties 类(此处为 api)指定了“加载”和“存储”方法,它们应该完全做到这一点。使用 FileInputStream 和 FileOutputStream 指定要将其保存到的文件。

回答by Pascal Thivent

You could use a very simple approach based on the java.util.Propertiesclass which has indeed a loadand storemethods that you can use in conjunction with a FileInputStream and FileOutputStream:

您可以使用基于java.util.Properties类的非常简单的方法,该类确实具有加载存储方法,您可以将它们与 FileInputStream 和 FileOutputStream 结合使用:

But actually, I'd recommend to use an existing configuration library like Commons Configuration(amongst others). Check the Properties Howtoto see how to load, save and automatically reload a properties file using its API.

但实际上,我建议使用现有的配置库,如Commons Configuration(等等)。查看Properties Howto以了解如何使用其 API 加载、保存和自动重新加载属性文件。

回答by Romain Linsolas

In addition to the loadand storemethod of the Propertiesclass, you can also use the Apache Commons Configuration library, which provides functions to easily manipulate configuration files (and not only .properties files).

除了类的loadandstore方法Properties,你还可以使用 Apache Commons Configuration 库,它提供了轻松操作配置文件(不仅仅是 .properties 文件)的功能。

回答by p3t0r

java.util.Properties doesn't provide runtime reloading out-of-the-box as far as I know. Commons Configurationprovides support for reloading configuration at runtime. The reload strategy can be configured by setting a ReloadingStrategy on the PropertiesConfiguration object. It also offers various other useful utilities for making your application configurable.

据我所知,java.util.Properties 不提供开箱即用的运行时重新加载。 Commons Configuration支持在运行时重新加载配置。可以通过在 PropertiesConfiguration 对象上设置 ReloadingStrategy来配置重新加载策略。它还提供了各种其他有用的实用程序,用于使您的应用程序可配置。

回答by Sanju Thomas

Apache common configuration API provided different strategies to reload property files at run time. FileChangedReloadingStrategy is one of them. Refer this linkto see an example for property file reloading at run time using FileChangedReloadingStrategy.

Apache 通用配置 API 提供了不同的策略来在运行时重新加载属性文件。FileChangedReloadingStrategy 就是其中之一。请参阅此链接以查看使用 FileChangedReloadingStrategy 在运行时重新加载属性文件的示例。

回答by David Pham

I completely agree that Apache Commons Configuration API is really good choice.

我完全同意 Apache Commons Configuration API 确实是不错的选择。

This example update properties at runtime

此示例在运行时更新属性

File propertiesFile = new File(getClass().getClassLoader().getResource(fileName).getFile());        
PropertiesConfiguration config = new PropertiesConfiguration(propertiesFile);           
config.setProperty("hibernate.show_sql", "true");
config.save();

From the post how to update properties file in Java

从帖子如何更新 Java 中的属性文件

Hope this help!

希望这有帮助!