Java获取Properties文件中值的方法

来源:互联网 发布:三国之数据辅助103 编辑:程序博客网 时间:2024/05/02 08:58

在遇到钉钉开发的时候需要将任务调度每次获取到的access_token赋给一个常量类中的静态属性,

需要获取到配置文件中的properties文件的值

  1. import java.io.BufferedInputStream;  
  2. import java.io.FileInputStream;  
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.Enumeration;  
  6. import java.util.Properties;  
  7. import java.util.PropertyResourceBundle;  
  8. import java.util.ResourceBundle;  
  9.   
  10. public class Test {  
  11.   
  12.     // 1。使用java.util.Properties 类的load()方法  
  13.   
  14.     // 示例:  
  15.   
  16.     public static String  getProperties_1(String url) throws IOException {  
  17.   
  18.         InputStream in = new BufferedInputStream(new FileInputStream(url));  
  19.   
  20.         Properties p = new Properties();  
  21.   
  22.         p.load(in);  
  23.   
  24.         return p.getProperty("jdbc.type");  
  25.   
  26.     }  
  27.   
  28.     // 2。使用java.util.ResourceBundle类的getBundle()方法  
  29.   
  30.     // 示例:  
  31.   
  32.     public static void getProperties_2(String url) {  
  33.         ResourceBundle rb = ResourceBundle.getBundle(url);  
  34.         Enumeration<String> keys = rb.getKeys();  
  35.         while (keys.hasMoreElements()){  
  36.               
  37.             System.out.println(rb.getString(keys.nextElement()));  
  38.         }  
  39.           
  40.     }  
  41.   
  42.     // 3。使用java.util.PropertyResourceBundle类的构造函数  
  43.   
  44.     // 示例:  
  45.   
  46.     public static void getProperties_3(String url) throws IOException {  
  47.   
  48.         InputStream in = new BufferedInputStream(new FileInputStream(url));  
  49.   
  50.         ResourceBundle rb = new PropertyResourceBundle(in);  
  51.         Enumeration<String> keys = rb.getKeys();  
  52.   
  53.         while (keys.hasMoreElements()){  
  54.               
  55.             System.out.println(rb.getString(keys.nextElement()));  
  56.         }  
  57.     }  
  58.   
  59.     // 4。使用class变量的getResourceAsStream()方法  
  60.   
  61.     // 示例:  
  62.   
  63.     public static String getProperties_4(String url) throws IOException {  
  64.   
  65.         InputStream in = Test.class.getResourceAsStream(url);  
  66.   
  67.         Properties p = new Properties();  
  68.   
  69.         p.load(in);  
  70.           
  71.         return p.getProperty("jdbc.url");  
  72.   
  73.     }  
  74.   
  75.     // 5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法  
  76.   
  77.     // 示例:  
  78.   
  79.     public static String getProperties_5(String url) throws IOException {  
  80.   
  81.         InputStream in = Test.class.getClassLoader().getResourceAsStream(url);  
  82.   
  83.         Properties p = new Properties();  
  84.   
  85.         p.load(in);  
  86.   
  87.         return p.getProperty("jdbc.url");  
  88.   
  89.     }  
  90.   
  91.     // 6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法  
  92.   
  93.     // 示例:  
  94.   
  95.     public static String getProperties_6(String url) throws IOException {  
  96.   
  97.         InputStream in = ClassLoader.getSystemResourceAsStream(url);  
  98.   
  99.         Properties p = new Properties();  
  100.   
  101.         p.load(in);  
  102.   
  103.         return p.getProperty("jdbc.url");  
  104.   
  105.     }  
  106.   
  107.     //  
  108.   
  109.     // 补充  
  110.   
  111.     // Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法  
  112.   
  113.     // 示例:  
  114.   
  115.     // public static Properties getProperties_8(String url) {  
  116.   
  117.     // InputStream in = context.getResourceAsStream(url);  
  118.   
  119.     // Properties p = new Properties ();  
  120.   
  121.     // p.load(in);  
  122.   
  123.     // }  
  124.   
  125.     public static void main(String[] args) throws IOException {  
  126.         //需要将文件放置到项目根目录下面  
  127.         System.out.println(getProperties_1("settings.properties"));  
  128.         System.out.println("----------");  
  129.         //需要将文件放置到class文件同级目录下面  
  130.         getProperties_2("settings");  
  131.         System.out.println("----------");  
  132.         getProperties_3("settings.properties");  
  133.         System.out.println("----------");  
  134.         System.out.println(getProperties_4("settings.properties"));  
  135.         System.out.println("----------");  
  136.         System.out.println(getProperties_5("settings.properties"));  
  137.         System.out.println("----------");  
  138.         System.out.println(getProperties_6("settings.properties"));  
  139.     }  
  140.   
二、Spring 的  @Value注解的方式

spring  xml文件需要的配置

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- 加载应用属性实例,可通过  @Value("#{APP_PROP['jdbc.driver']}") String jdbcDriver 方式引用 -->  
  2.    <util:properties id="APP_PROP" location="classpath:settings.properties" local-override="true"/>  
java代码:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package cn.yufu.system.modules.oa.merchant.web;  
  2.   
  3. import cn.yufu.system.common.web.BaseController;  
  4. import cn.yufu.system.modules.oa.merchant.service.ActBusApprovalService;  
  5.   
  6. @Controller  
  7. @RequestMapping(value = "${adminPath}/merchant/actBusApproval")  
  8. public class ActBusApprovalController extends BaseController {  
  9.   
  10.     @Autowired  
  11.     private ActBusApprovalService actBusApprovalService;  
  12.     // APP_PROP  与配置文件的id相同  
  13.     @Value("#{APP_PROP['fileUploadPath']}")  
  14.     private String uploadFilePath;  
  15.       
  16.   
  17. }