免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4170 | 回复: 1
打印 上一主题 下一主题

Java Properties 属性文件读取写入工具类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-15 16:46 |只看该作者 |倒序浏览
Java Properties 属性文件读取写入工具类









自己编写的一个简单工具类,方便操作属性文件

构造方法必须传入属性文件的位置,工具类会自动定位到项目的物理根目录到/Webroot后面需要传入路径,传入的路径记住以/开头。

##现在工具类未对太多的安全性验证。



Java代码
  1. 1.package cn.net.withub.smsp.common.utils;   
  2. 2.  
  3. 3.import java.io.File;   
  4. 4.import java.io.FileInputStream;   
  5. 5.import java.io.FileOutputStream;   
  6. 6.import java.io.InputStream;   
  7. 7.import java.util.Properties;   
  8. 8.  
  9. 9.import org.apache.commons.logging.Log;   
  10. 10.import org.apache.commons.logging.LogFactory;   
  11. 11.  
  12. 12.import bsh.This;   
  13. 13.  
  14. 14.import cn.net.withub.lab.SMGPLab;   
  15. 15.  
  16. 16./**  
  17. 17. * 配置文件读取和写入工具类  
  18. 18. * @author Jacky.Jiang  
  19. 19. *  
  20. 20. */  
  21. 21.public class PropertiesHelper {   
  22. 22.    private static Log logger = LogFactory.getLog(This.class);   
  23. 23.      
  24. 24.    private String project_root = "";   
  25. 25.    private File file = null;   
  26. 26.  
  27. 27.      
  28. 28.    public PropertiesHelper(String filePath) {   
  29. 29.        //构造时获取到项目的物理根目录   
  30. 30.        project_root = this.getClass().getResource("/").toString().replace("file:/", "");   
  31. 31.        project_root = project_root.substring(0,project_root.indexOf("/WEB-INF"));   
  32. 32.           
  33. 33.        if(filePath != null && filePath.length() > 0){   
  34. 34.            try {   
  35. 35.                file = new File(project_root+filePath);   
  36. 36.                  
  37. 37.            } catch (Exception e) {   
  38. 38.                logger.error(e);   
  39. 39.            }   
  40. 40.        }   
  41. 41.    }   
  42. 42.      
  43. 43.    public String getProperties(String key){   
  44. 44.        InputStream fis = null;   
  45. 45.        try {   
  46. 46.            Properties prop = new Properties();   
  47. 47.            fis = new FileInputStream(getAbsolutePath());   
  48. 48.               
  49. 49.            prop.load(fis);   
  50. 50.               
  51. 51.            return prop.getProperty(key);   
  52. 52.               
  53. 53.        } catch (Exception e) {   
  54. 54.            logger.error(e);   
  55. 55.        }finally{   
  56. 56.            try{if(fis != null){fis.close();}}catch(Exception e){}   
  57. 57.        }   
  58. 58.           
  59. 59.        return "";   
  60. 60.    }   
  61. 61.      
  62. 62.    public void setProperties(String key,String value)throws Exception{   
  63. 63.        Properties prop = new Properties();   
  64. 64.           
  65. 65.           
  66. 66.        FileOutputStream outputFile = null;   
  67. 67.        InputStream fis = null;   
  68. 68.        try {   
  69. 69.            //输入流和输出流要分开处理, 放一起会造成写入时覆盖以前的属性   
  70. 70.            fis = new FileInputStream(getAbsolutePath());   
  71. 71.            //先载入已经有的属性文件   
  72. 72.            prop.load(fis);   
  73. 73.               
  74. 74.            //追加新的属性   
  75. 75.            prop.setProperty(key, value);   
  76. 76.               
  77. 77.            //写入属性   
  78. 78.            outputFile = new FileOutputStream(getAbsolutePath());   
  79. 79.            prop.store(outputFile, "");   
  80. 80.               
  81. 81.            outputFile.flush();   
  82. 82.        } catch (Exception e) {   
  83. 83.            logger.error(e);   
  84. 84.            throw e;   
  85. 85.        }finally{   
  86. 86.            try{if(fis != null){fis.close();}}catch(Exception e){}   
  87. 87.            try{if(outputFile != null){outputFile.close();}}catch(Exception e){}   
  88. 88.        }   
  89. 89.    }   
  90. 90.      
  91. 91.      
  92. 92.    public String getAbsolutePath(){   
  93. 93.        try {   
  94. 94.            return file.getAbsolutePath();   
  95. 95.        } catch (Exception e) {   
  96. 96.            logger.error(e);   
  97. 97.        }   
  98. 98.        return "";   
  99. 99.    }   
  100. 100.}  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-15 16:46 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP