解决修改properties 属性文件存在缓存问题,附带操作properties文件工具类

来源:互联网 发布:软件报价单下载 编辑:程序博客网 时间:2024/06/06 10:51

在做项目的时候有些数据不一定需要在数据库管理,例如数据库连接,定时任务等等的配置..有时候需要动态修改这些数据,但在修改完后,再次获取时出现问题.

   在项目中要修改properties,修改之后,再进入相关目录查看properties文件,发现内容已经修改了,但是但通过TaskController.class.getResourceAsStream("/config.properties");获取的数据时,还是没有改变前的数据.

   原因是:.getResourceAsStream是通过缓存中获取的.

   解决办法:能过真实路径获取TaskController.class.getResource("/config.properties").getPath();

 

操作properties文件工具类:

[java] view plain copy
  1. package com.lanyuan.video.util;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.util.Iterator;  
  8. import java.util.Properties;  
  9. import java.util.Map.Entry;  
  10. import com.lanyuan.video.task.TaskController;  
  11.   
  12. public class PropertiesUtils {  
  13.     /** 
  14.      * 获取属性文件的数据 根据key获取值 
  15.      * @param fileName 文件名 (注意:加载的是src下的文件,如果在某个包下.请把包名加上) 
  16.      * @param key 
  17.      * @return 
  18.      */  
  19.     public static String findPropertiesKey(String key) {  
  20.           
  21.         try {  
  22.             Properties prop = getProperties();  
  23.             return prop.getProperty(key);  
  24.         } catch (Exception e) {  
  25.             return "";  
  26.         }  
  27.           
  28.     }  
  29.   
  30.     public static void main(String[] args) {  
  31.         Properties prop = new Properties();  
  32.         InputStream in = TaskController.class  
  33.                 .getResourceAsStream("/config.properties");  
  34.         try {  
  35.             prop.load(in);  
  36.             Iterator<Entry<Object, Object>> itr = prop.entrySet().iterator();  
  37.             while (itr.hasNext()) {  
  38.                 Entry<Object, Object> e = (Entry<Object, Object>) itr.next();  
  39.                 System.err.println((e.getKey().toString() + "" + e.getValue()  
  40.                         .toString()));  
  41.             }  
  42.         } catch (Exception e) {  
  43.               
  44.         }  
  45.     }  
  46.   
  47.     /** 
  48.      * 返回 Properties 
  49.      * @param fileName 文件名 (注意:加载的是src下的文件,如果在某个包下.请把包名加上) 
  50.      * @param  
  51.      * @return 
  52.      */  
  53.     public static Properties getProperties(){  
  54.         Properties prop = new Properties();  
  55.         String savePath = TaskController.class.getResource("/config.properties").getPath();  
  56.         //以下方法读取属性文件会缓存问题  
  57. //      InputStream in = TaskController.class  
  58. //              .getResourceAsStream("/config.properties");  
  59.         try {  
  60.             InputStream in =new BufferedInputStream(new FileInputStream(savePath));    
  61.             prop.load(in);  
  62.         } catch (Exception e) {  
  63.             return null;  
  64.         }  
  65.         return prop;  
  66.     }  
  67.     /** 
  68.      * 写入properties信息 
  69.      *  
  70.      * @param key 
  71.      *            名称 
  72.      * @param value 
  73.      *            值 
  74.      */  
  75.     public static void modifyProperties(String key, String value) {  
  76.         try {  
  77.             // 从输入流中读取属性列表(键和元素对)  
  78.             Properties prop = getProperties();  
  79.             prop.setProperty(key, value);  
  80.             String path = TaskController.class.getResource("/config.properties").getPath();  
  81.             FileOutputStream outputFile = new FileOutputStream(path);  
  82.             prop.store(outputFile, "modify");  
  83.             outputFile.close();  
  84.             outputFile.flush();  
  85.         } catch (Exception e) {  
  86.         }  
  87.     }  
  88. }  
0 0
原创粉丝点击