JAVA:外部配置文件读取类

package com.utility;
 

import java.io.*;
import java.util.Properties;
 
/** 
 * 读取实时的配置文件信息
 */  
public class SysProperties {
 
 
	private static Properties SysLocalPropObject = null;
 
	//配置文件路径
	private static String defaultPropFileName = "config.properties";
	//文件更新标识
	protected long lastModifiedData = -1;
	
	private static SysProperties instance;
	
	
//	public static void main(String[] args){
//		SysProperties properties = getInstance("config.properties");
//		String filename = properties.getProperty("filename");
//		System.out.println(filename);
//	}
	
	
	public static SysProperties getInstance(String fileName){
		 if(instance == null){
			 if(fileName.length()!=0){
				 defaultPropFileName=fileName;
			 }
			 instance = new SysProperties();
		 }
		 return instance;
	}
	
 
	/**
	 * @description 私有构造器启动一个线程实时监控配置文件
	 */
	private SysProperties() {
		SysLocalPropObject = new Properties();
//		String tempPath = this.getClass().getResource(defaultPropFileName).getFile();
		String tempPath = System.getProperty("user.dir") + File.separator + defaultPropFileName;
		System.out.println(tempPath);
		File tempFile = new File(tempPath);
		final String filePath;
		if(tempFile.exists()) {
			filePath = tempPath;
		} else {
			filePath = "system.properties";
		}
		
		final SysProperties self = this;
		File propertyFile = new File(filePath);
		if (propertyFile.exists()) {reloadFile(propertyFile);}
 
		//循环监控配置文件的变化,一旦发现文件发生变化了则重读配置文件
		Thread t = new Thread() {
			@Override
			public void run() {
				while (true) {
					//间隔1秒
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
					}
					
					try {
						File propertyFile = new File(filePath);
						if (self.lastModifiedData != propertyFile.lastModified()) {
							self.reloadFile(propertyFile);
							self.lastModifiedData = propertyFile.lastModified();
						}
					} catch (Exception e) {
 
					}
				}
			}
		};
		t.start();
	}
 
	/** 
	 * 重新加载文件
	 * @param propertyFile
	 */ 
	private void reloadFile(File propertyFile) {
		FileInputStream inputStreamLocal = null;
		try {
			inputStreamLocal = new FileInputStream(propertyFile);
			InputStreamReader reader = new InputStreamReader(inputStreamLocal,"utf-8");
			SysLocalPropObject.load(reader);
		} catch (Exception e) {
			if (e instanceof FileNotFoundException) {
				e.printStackTrace();
				SysLocalPropObject = null;
			} else {
				e.printStackTrace();
			}
		} finally {
			try {
				if (inputStreamLocal != null)
					inputStreamLocal.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
 
 
	/** 
	 * 根据key获取value
	 * @param property
	 * @return String
	 */ 
	public String getProperty(String property) {
		String val = null;
 
		if (SysLocalPropObject != null) {
            val = SysLocalPropObject.getProperty(property);
        }
		return (val);
 
	}
	/**
	 * 根据键修改值
	 * @param key
	 * @param value
	 *
	 */
	public void setProperty(String key,String value) {

		if (SysLocalPropObject != null){
//			String tempPath = this.getClass().getResource(defaultPropFileName).getFile();
			String tempPath = System.getProperty("user.dir") + File.separator + defaultPropFileName;
			OutputStream os;
			try {
				os = new FileOutputStream(tempPath);
				SysLocalPropObject.setProperty(key, value);
				SysLocalPropObject.store(os, "Update "+key+" "+value);
				os.close();
			}
			catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
 
	/** 
	 * 根据key获取value
	 * @param property
	 * @param defaultValue 指定默认值
	 * @return String
	 */ 
	public String getProperty(String property, String defaultValue) {
		String val = null;
 
		if (SysLocalPropObject != null) {
			val = SysLocalPropObject.getProperty(property, defaultValue);
		} else {
			val = defaultValue;
		}
 
		return (val);
	}
 
	/** 
	 * 根据key获取value
	 * @param property
	 * @return Integer
	 */ 
	public Integer getIntProperty(String property) {
		String val = getProperty(property);
		Integer nVal = null;
		if (val != null) {
			try {
				nVal = Integer.parseInt(val);
			} catch (Exception e) {
	
			}
		}
		return nVal;
 
	}
 
	/** 
	 * 根据key获取value
	 * @param property
	 * @param defaultValue 指定默认值
	 * @return Integer
	 */ 
	public Integer getIntProperty(String property, Integer defaultValue) {
		Integer val = getIntProperty(property);
 
		if (val == null) {
			val = defaultValue;
		}
 
		return (val);
	}
 
	/** 
	 * 根据key获取value
	 * @param property
	 * @return
	 */ 
	public Long getLongProperty(String property) {
		String val = getProperty(property);
		Long nVal = null;
		try {
			nVal = Long.parseLong(val);
		} catch (Exception e) {
 
		}
		return nVal;
 
	}
 
	/** 
	 * 根据key获取value
	 * @param property
	 * @param defaultValue
	 * @return
	 */ 
	public Long getLongProperty(String property, Long defaultValue) {
		Long val = getLongProperty(property);
 
		if (val == null) {
			val = defaultValue;
		}
 
		return (val);
	}
 
	/** 
	 * 根据key获取value
	 * @param property
	 * @param defaultValue
	 * @return
	 */ 
	public boolean getBooleanProperty(String property, boolean defaultValue) {
		boolean retval = false;
		String val = getProperty(property);
 
		if (val == null || val.equals(""))
			retval = defaultValue;
		else if (val.trim().equalsIgnoreCase("true") || val.trim().equals("1"))
			retval = true;
 
		return (retval);
	}
	
	/** 
	 * 根据key获取value
	 * @param property
	 * @return
	 */ 
	public Double getDoubleProperty(String property) {
		String val = getProperty(property);
		Double nVal = null;
		try {
			nVal = Double.parseDouble(val);
		} catch (Exception e) {
		}
		return nVal;
	}
 
 
	/** 
	 * 根据key获取value
	 * @param property
	 * @param defaultValue
	 * @return
	 */ 
	public Double getDoubleProperty(String property, Double defaultValue) {
		Double val = getDoubleProperty(property);
		if (val == null) {
			val = defaultValue;
		}
		return (val);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肆意飞扬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值