Properties 盛放配置及监控变化

本文介绍Java中Properties类的使用方法,包括从文件加载属性、修改并保存属性等操作。同时提供了一个封装好的Config类实现自动刷新配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Properties类的层次结构
java.lang.Object
  java.util.Dictionary<K,V>
      java.util.Hashtable<Object,Object>
          java.util.Properties

properties文件是一个文本文件,注释格式为#XXX;正文配置为A=B。

 void java.util.Properties.load(InputStream inStream) throws IOException
从输入字节流中读取一个属性列表。
Object java.util.Hashtable.put(Object key, Object value)
指定新映射。返回原key映射的值。若不存在返回null。
String java.util.Properties.getProperty(String key)
返回key对应的value。

例子

import java.io.*; 
import java.util.Date;
import java.util.Properties; 

 
public class TestProperties { 
        public static void main(String args[]) throws IOException { 
                test(); 
        } 

        public static void test() throws IOException { 
                InputStream is = new FileInputStream("D:\\pp.properties"); 
                Properties prop = new Properties(); 
                prop.load(is); 
                //循环输出配置信息 
                for (Object key : prop.keySet()) { 
                        System.out.println(key + "=" + prop.get(key)); 
                } 
                is.close();

                //修改Properties对象,并持久化到一个文件 
//                prop.put("c", 5); 
//                OutputStream os = new FileOutputStream("D:\\pp.properties"); 
//                String str="saved on "+ new Date();
//                prop.store(os,str);
//                os.close(); 
        } 
}
/*b=2
a=1*/

文件与资源

有时我们会把配置文件也放到jar包内,此时用File类读取就不行了,需要用下列函数:
InputStream java.lang.ClassLoader.getResourceAsStream(String name)
使用它还有一个好处,在开发调试时与最终运行时都不需要再改读配置的路径啦。

封装好的config类

单例模式。
package com.yichudu.yichuutil;

import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class Config {
	private static Config instance = null;
	private Properties properties = new Properties();
	private boolean refreshEnable;
	private int reFreshInterval=1;
	private long lastRefreshTimestamp=0;
	private String path;
	
	public void enableRefresh(int reFreshIntervalBySecond){
		refreshEnable=true;
		reFreshInterval=reFreshIntervalBySecond;
	}
	
	private Config() {
	}

	synchronized public static Config getInstance() {
		if (instance == null) 
			instance = new Config();
		return instance;
	}

//	public void loadResource(String path) throws IOException {
//		InputStream is = Config.class.getClassLoader()
//				.getResourceAsStream(path);
//		InputStreamReader isr = new InputStreamReader(is, "UTF-8");
//		properties.load(isr);
//	}

	public void loadFile(String path) throws IOException {
		this.path=path;
		properties.load(new FileReader(path));
	}

	public String get(String key) {
		try{
			if(refreshEnable)
				refreshCheck();
		}catch(Exception e){
			return properties.getProperty(key);
		}
		return properties.getProperty(key);
	}

	public String get(String key, String defaultValue) {
		try{
			if(refreshEnable)
				refreshCheck();
			}catch(Exception e){
				return properties.getProperty(key) == null ? defaultValue : properties
						.getProperty(key);
			}
		return properties.getProperty(key) == null ? defaultValue : properties
				.getProperty(key);
	}
	
	void refreshCheck() throws IOException{
		if(System.currentTimeMillis()-lastRefreshTimestamp>reFreshInterval*1000){
			lastRefreshTimestamp=System.currentTimeMillis();
			loadFile(path);
		}
	}

	@Override
	public String toString() {
		return properties.toString();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值