如何在java中重新加载属性文件 [英] How to reload properties file in java

查看:561
本文介绍了如何在java中重新加载属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人请帮我重新加载属性文件。
我有这样的代码

Any one please help me on reloading properties file. i have the code like this

Properties prop = new Properties();
InputStream trackerFileStream = 
    LoadProperty.class.getClassLoader().getResourceAsStream("sample.properties");
prop.load(trackerFileStream);

通过类加载器加载属性文件后,我修改了属性文件。
无法在同一程序执行中获得最新修改。

After loading the properties file through class loader, i modify the properties file. Am not able to get the latest modifications in the same program execution.

任何人都可以建议我如何重新加载属性文件而不重新执行程序。

Can any one please suggest me how to reload the properties file without re-executing the program.

推荐答案

如果您想要更新,您需要检查从时间到资源的资源并重新加载属性。

If you want to be updated you need to check the resource from tome to time and reload the properties.

从classpath加载属性是个问题。如果使用文件系统,则可以检查文件的最后修改属性并决定是否重新加载它。在java 7中,你可以注册监听器,它会在修改文件时回叫你。加载资源时无法执行此操作。

It is a problem that you are loading properties from classpath. If you use file system you can check the last modified attribute of file and decide whether to reload it. In java 7 you evern can register listener that will call you back when file is modified. You can't do this when loading resource.

但你可以做得更好。使用apache中的配置包。它已经实现了重新加载逻辑,因此您可以对抗实际映射到资源的配置,并确保始终获得更新的数据。

But you can do better. Use configuration package from apache. It already implements reloading logic, so you just can work against "configuration" actually mapped to resource and be sure you always get updated data.

这篇关于如何在java中重新加载属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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