读取properties配置文件的工具类

转载:https://blog.csdn.net/sdsky1987/article/details/7418101

一般小工程,properties配置文件之间在src根目录创建

比如data.properties,代码如下

[java] view plain copy
  1. <!-- 代理服务器地址 -->  
  2. PROXY=192.168.0.1  
  3. <!-- 代理服务器端口号 -->  
  4. PORT=8080  
  5. <!-- 登录代理的用户名 -->  
  6. USERNAME=username  
  7. <!-- 登录代理的口令 -->  
  8. PASSWORD=password  
  9. <!-- 图片存储路径 -->  
  10. imgPath=c:/xml/  
  11. <!-- xml文件名 -->  
  12. xmlName=UploadRequest.xml  
  13.   
  14. <!-- 1. Build Fat Jar包含dom4j -->  
  15. <!-- 2. cmd - 生成的jar路径下 java -jar xx.jar -->  
  16. <!-- properties配置文件更新困难 -->  

用于读取数据的工具类如下写:

[java] view plain copy
  1. package com.main.util;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Properties;  
  5.   
  6. /** 
  7.  *  
  8.  * 读取properties文件的工具类 
  9.  * 
  10.  * @author 莫小哆_ly 2012-3-30 
  11.  */  
  12. public class Tools {  
  13.     private static Properties p = new Properties();  
  14.   
  15.     /** 
  16.      * 读取properties配置文件信息 
  17.      */  
  18.     static{  
  19.         try {  
  20.             p.load(Tools.class.getClassLoader().getResourceAsStream("data.properties"));  
  21.         } catch (IOException e) {  
  22.             e.printStackTrace();   
  23.         }  
  24.     }  
  25.     /** 
  26.      * 根据key得到value的值 
  27.      */  
  28.     public static String getValue(String key)  
  29.     {  
  30.         return p.getProperty(key);  
  31.     }  
  32. }  

如此,调用配置文件中常量的时候,只要调用getValue()方法即可,比如

[java] view plain copy
  1. Tools.getValue("PORT")  

即 8080


========================================================================


另外,项目中也经常单独将一部分功能独立做Java Project,然后打成jar包供其他项目调用。如果jar包中需要读取配置文件信息,则很少把该配置打进jar包,因为它不方便修改,更多都是采用jar包读取外部配置文件。

properties配置文件从工程移除,先放在工程下、与src并列路径。如图


读取配置文件的工具类Tools做如下改动:

[java] view plain copy
  1. package com.main.util;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.util.Properties;  
  8.   
  9. /** 
  10.  *  
  11.  * 读取properties文件的工具类 
  12.  *  
  13.  * @author 莫小哆_ly 2012-3-30 
  14.  */  
  15. public class Tools {  
  16.   
  17.     private static Properties p = new Properties();  
  18.     static {  
  19.         try {  
  20.             // System.getProperty("user.dir") 获得项目的绝对路径,然后拼装配置文件的路径  
  21.             // 读取系统外配置文件 (即Jar包外文件) --- 外部工程引用该Jar包时需要在工程下创建config目录存放配置文件  
  22.             String filePath = System.getProperty("user.dir") + "/config/data.properties";  
  23.             InputStream in = new BufferedInputStream(new FileInputStream(filePath));  
  24.             p.load(in);  
  25.         } catch (IOException e) {  
  26.             System.out.println("读取配置信息出错!");  
  27.         }  
  28.     }  
  29.   
  30.     /** 
  31.      * 根据key得到value的值 
  32.      */  
  33.     public static String getValue(String key) {  
  34.         return p.getProperty(key);  
  35.     }  
  36. }  

打成jar包时,先移除config

利用Fat Jar打包,将生成的jar包与config文件夹放在同一路径即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值