Java Properties无需重启服务器即可动态读取最新键值

本文介绍了一种在Java项目中动态读取和更新properties文件的方法,避免了每次修改配置后都需要重启服务器的问题。通过直接使用文件输入流而非类加载器流,确保能够实时获取到最新的配置信息。

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

转载自

最近项目中需要用到动态读取properties文件,但是最后测试发现,每次修改键值之后,必须要重启服务器才会读取到最新修改的值,网上查了很多资料,也没人能完整解答,最后综合网上部分方案,终于发现问题所在,不用多解释,直接上代码,分享出来希望能帮助到遇到同样问题的程序猿们大笑

[java]  view plain copy
  1. package xxx.xxx.xxx.xxx;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.OutputStream;  
  7. import java.util.Properties;  
  8. /** 
  9.  * <p>Description: properties文件操作工具</p> 
  10.  * @author cs 
  11.  */  
  12. public class PropertiesUtil {  
  13.     private String paramsPath = "params.properties";  
  14.     private String counterPath = "counter.properties";  
  15.   
  16.     /** 
  17.      * <p>Description: 根据key读取value</p> 
  18.      * @author cs 
  19.      * @version 2013-10-24 
  20.      * @modifier cs 
  21.      * @modifiDate 2013-10-24 
  22.      * <p>modifiContent: 首次创建</p> 
  23.      * @param isParamProper 是否是读取参数配置信息,true是,false则读取计数器配置 
  24.      * @param key 
  25.      * @return 
  26.      */  
  27.     public String readValue(boolean isParamProper, String key) {  
  28.         Properties props = new Properties();  
  29.         String path = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1);  
  30.         FileInputStream fis = null;  
  31.         try {  
  32.               
  33.             fis = new FileInputStream(path+(isParamProper?paramsPath:counterPath));  
  34.             //不能用以下的方式,否则必须要重启服务器才能读取到最新的数据,问题就出在这  
  35. //          InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(isParamProper?paramsPath:counterPath);  
  36. //          props.load(in);  
  37.             props.load(fis);  
  38.             String value = props.getProperty(key);  
  39.             return value;  
  40.         } catch (Exception e) {  
  41.             e.printStackTrace();  
  42.         } finally {  
  43.             if(null!=fis) {  
  44.                 try {  
  45.                     fis.close();  
  46.                 } catch (IOException e) {  
  47.                     e.printStackTrace();  
  48.                 }  
  49.             }  
  50.         }  
  51.         return null;  
  52.     }  
  53.   
  54.       
  55.     /** 
  56.      * <p>Description: 写入properties信息</p> 
  57.      * @author cs 
  58.      * @version 2013-10-24 
  59.      * @modifier cs 
  60.      * @modifiDate 2013-10-24 
  61.      * <p>modifiContent: 首次创建</p> 
  62.      * @param isParamProper 是否是写入参数配置信息,true是,false则写入计数器配置 
  63.      * @param parameterName 
  64.      * @param parameterValue 
  65.      * @throws IOException  
  66.      */  
  67.     public void writeProperties(boolean isParamProper,String parameterName,  
  68.             String parameterValue) {  
  69.         Properties prop = new Properties();  
  70.         String path = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1);  
  71.           
  72.         FileInputStream fis = null;  
  73.         OutputStream fos = null;  
  74.         try {  
  75. //          fis = Thread.currentThread().getContextClassLoader().getResourceAsStream(isParamProper?paramsPath:counterPath);  
  76.             // 从输入流中读取属性列表(键和元素对)  
  77.             fis = new FileInputStream(path+(isParamProper?paramsPath:counterPath));  
  78.             prop.load(fis);  
  79.             fis.close();//一定要在修改值之前关闭fis  
  80.               
  81.             fos = new FileOutputStream(path+(isParamProper?paramsPath:counterPath));  
  82.             prop.setProperty(parameterName, parameterValue);  
  83.               
  84.             prop.store(fos, null);//第二个参数为注释  
  85.             fos.close();  
  86.         } catch (IOException e) {  
  87.             e.printStackTrace();  
  88.         } finally {  
  89.             if(null!=fos) {  
  90.                 try {  
  91.                     fos.close();  
  92.                     if(null!=fis) fis.close();  
  93.                 } catch (IOException e) {  
  94.                     e.printStackTrace();  
  95.                 }  
  96.             }  
  97.         }  
  98.     }  
  99.       
  100. }  

另外需要说明的是尽量别用静态的方式写读取和写入方法,每次调用时,重新new该对象更妥。

本示例中,properties文件默认放在classes目录下。

    public String readValue(String key){
  		Properties props = new Properties();
  		String path = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1);
  		System.err.println(path); 
  		FileInputStream fis = null;
  		try {
  			fis = new FileInputStream(path + "checkname.properties");
  			InputStreamReader sr = new InputStreamReader(fis,"UTF-8"); 
  			props.load(sr);
  			String value = props.getProperty(key);
  			return value;
  		} catch (Exception e) {
  			logger.error("error: " + e); 
  		} finally {
  			if(null!=fis) {
  				try {
  					fis.close();
  				} catch (IOException e) {
  					logger.error("error: " + e); 
  				}
  			}
  		}
  		return null;
   }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值