在实际的项目中有些数据是临时数据不必要保存到数据库中因此保存到properties配置文件中也是不错的选择。
但是保存在文件中的弊端就是如果要修改就需要修改配置文件了,做成可视化的配置界面有点偏移主题了。
以下代码参考的log4j-x-x.jar中的文件并做少许更改。
具体代码程序实现:扫描文件类FileWatchdog.java
import java.io.File; /** Check every now and then that a certain file has not changed. If it has, then call the {@link #doOnChange} method. @author Ceki Gülcü @since version 0.9.1 @update zhangyq @date 2014-4-28 10:54:00 */ public abstract class FileWatchdog extends Thread { /** The default delay between every file modification check, set to 60 seconds. */ static final public long DEFAULT_DELAY = 60000; /** The name of the file to observe for changes. */ protected String filename; /** The delay to observe between every check. By default set {@link #DEFAULT_DELAY}. */ protected long delay = DEFAULT_DELAY; File file; long lastModif = 0; boolean warnedAlready = false; boolean interrupted = false; protected FileWatchdog(String filename) { this.filename = filename; file = new File(filename); setDaemon(true); checkAndConfigure(); } /** Set the delay to observe between each check of the file changes. */ public void setDelay(long delay) { this.delay = delay; } abstract protected void doOnChange(); protected void checkAndConfigure() { boolean fileExists; try { fileExists = file.exists(); } catch (SecurityException e) { interrupted = true; // there is no point in continuing return; } if (fileExists) { long l = file.lastModified(); // this can also throw a SecurityException if (l > lastModif) { // however, if we reached this point this lastModif = l; // is very unlikely. doOnChange(); warnedAlready = false; } } else { if (!warnedAlready) { warnedAlready = true; } } } public void run() { while (!interrupted) { try { Thread.sleep(delay); } catch (InterruptedException e) { // no interruption expected } checkAndConfigure(); } } }
该类监测是否修改并提供一个抽象方法
abstract protected void doOnChange();
在文件修改后要做的下一步操作。
具体的业务需求是:将配置文件中的数据保存到一个Map<String,String>集合中一旦修改了该配置文件集合中的数据同时也做修改,具体代码实现:
/** * Info: 动态加载*.properties文件 * Date: 2014-4-28 14:22:54 * @author zhangyq * */ public class PropertiesFileDynamicLoadingUtil { private static Map<String, String> fileProperty; public static Map<String, String> getFileProperty() { return fileProperty; } /** * * @param configFilename 文件的绝对路径 * @param delay 检验文件的间隔时间段 */ public static void configureAndLoad(String configFilename, long delay) { PropertyWatchdog pdog = new PropertyWatchdog(configFilename); pdog.setDelay(delay); pdog.start(); } //-------------------------------------- 静态内部类定义 -------------------------- /** * Info: 动态读取配置文件的信息 * @author zhangyq */ static class PropertyWatchdog extends FileWatchdog { /** * 加载配置文件 * @param props * @return */ public static Map<String, String> loadProperites(Properties props){ fileProperty = new HashMap<String, String>(); Set<Entry<Object, Object>> entrySet = props.entrySet(); for (Entry<Object, Object> entry : entrySet) { fileProperty.put(((String) entry.getKey()).trim(), ((String) entry.getValue()).trim()); } return fileProperty; } /** * 构造函数 * @param configFileName 绝对路径 */ PropertyWatchdog(String filename){ super(filename); } @Override protected void doOnChange() { Properties props = new Properties(); FileInputStream istream = null; try { istream = new FileInputStream(filename); props.load(istream); istream.close(); } catch (Exception e) { return; } finally { if(istream != null) { try { istream.close(); } catch(Throwable ignore) { } } } // TODO loadProperites(props); } } //--------------------------------------end 静态内部类定义 end-------------------------- }
具体测试和正式调用代码为:path参数为绝对路径
PropertiesFileDynamicLoading.configureAndLoad(path, 15); Map<String, String> map = PropertiesFileDynamicLoading.getFileProperty();
相关推荐
java 改变Properties文件中的键值 java 改变Properties文件中的键值
主要介绍了在python中修改.properties文件的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
主要介绍了Java实现的properties文件动态修改并自动保存工具类,可实现针对properties配置文件的相关修改与保存功能,需要的朋友可以参考下
java配置文件修改读取工具类,包括yml和properties类型文件
本篇文章主要介绍了Java 操作Properties配置文件详解,详细的介绍了Properties和主要方法,有兴趣的可以了解下
这是一个log4j配置文件,可以在控制台打印输出debug信息,方便项目调试,无需修改,拿来即用,放在项目的classpath目录下即可。
读取并正常显示配置文件(.properties),保存修改之后的信息;包含源码和运行命令,
主要介绍了Python实现加载及解析properties配置文件的方法,结合实例形式分析了Python针对properties配置文件的加载、读取及解析相关操作技巧,需要的朋友可以参考下
ava中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去...
由于在工作中经常要使用.properties配置文件,并且配置文件的属性也需要在文件修改后及时更新,同时读取src文件夹下的配置文件时也存在相对路径与绝对路径的转换问题,还有国际化问题等,因此我编写了一个配置文件...
spark2.3的配置文件log4j.properties,修改部分是为了只显示ERROR信息,隐藏INFO信息
ssm一个简单项目所需的xml配置文件,spring+springmvc+mybatis框架中用到了三个XML配置文件:web.xml,spring-mvc.xml,spring-mybatis.xml.第一个不用说,每个web项目都会有的也是关联整个项目的配置.第二个文件spring-...
.net、C#、读取指定路径的配置文件配置文件。如配置文件如下: [UserInfo] Id=1001 Name=de 可以读取其Id的值以及修改都可以的
有数据库连接池druid-1.1.21.jar、druid-1.1.21.jar和配置文件druid.properties写好了 自己修改下密码
作为Apache的开源项目,Log4j可以使我们很好的控制日志信息输送的目的地是控制台、文件、GUI组件、甚至是套接口服务器、NT的...最令人感 兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码
这是做项目中,集成第三东西需要些配置,我抽取到属性文件中,然后写了个公用类统一调用,基本就是key返回具体值,属性文件位置自己可以调整,然后类中修改获取路径就可以了。目前我的属相文件放置在src下
本资源是解决写Properties出现中文乱码的源代码,不用作任何修改就可以将中文写入Properties,跟乱码说拜拜
配置文件的作用就是来配置某些信息,修改某些默认值,在我们快速创建的springboot项目中就是Application.properties 这里引入一种新的配置文件的形式就是yaml配置文件,后缀名为yml 它以数据为中心,比json和xml更高效 ...
文件类型包括36个JavaScript文件、23个Java源代码文件、18个CSS样式文件、14个HTML页面文件、12个Map文件、12个PNG图片文件、9个XML配置文件、9个GIF图片文件、5个Properties配置文件和4个YAML配置文件。该系统适合...