zoukankan      html  css  js  c++  java
  • 六种获取配置properties文件的方法

    总结一下六种获取配置properties文件的方法,代码如下:

      1 package com.xujingyang.test ;
      2 
      3 import java.io.BufferedInputStream ;
      4 import java.io.FileInputStream ;
      5 import java.io.InputStream ;
      6 import java.util.Locale ;
      7 import java.util.Properties ;
      8 import java.util.PropertyResourceBundle ;
      9 import java.util.ResourceBundle ;
     10 import org.junit.Test ;
     11 
     12 /**
     13  * @descript  六种获取配置文件的方法,注意各种的路径的问题
     14  * @author    xujingyang
     15  * @time      2017年5月15日下午4:23:18
     16  */
     17 public class TestReadProperties {
     18     
     19     /**
     20      * 1、使用java.util.Properties类的load()方法
     21      */
     22     @Test
     23     public void read1(){
     24         try {
     25             InputStream stream=new BufferedInputStream(new FileInputStream("src/my.properties"));
     26             Properties p=new Properties();
     27             p.load(stream);
     28             String name = p.get("name").toString() ;
     29             String age = p.get("age").toString() ;
     30             System.out.println(name+":"+age) ;
     31         } catch (Exception e) {
     32             e.printStackTrace();
     33         }
     34     }
     35     
     36     /**
     37      * 2、使用java.util.ResourceBundle类的getBundle()方法
     38      */
     39     @Test
     40     public void read2(){
     41         try {
     42             ResourceBundle bundle = ResourceBundle.getBundle("my", Locale.getDefault()) ;
     43             String name = bundle.getString("name");
     44             String age = bundle.getString("age");
     45             System.out.println(name+":"+age) ;
     46         } catch (Exception e) {
     47             e.printStackTrace();
     48         }
     49     }
     50     
     51     
     52     
     53     /**
     54      *3、 使用java.util.PropertyResourceBundle类的构造函数
     55      */
     56     @Test
     57     public void read3(){
     58         try {
     59             InputStream stream=new BufferedInputStream(new FileInputStream("src/my.properties"));
     60             ResourceBundle bundle=new PropertyResourceBundle(stream);
     61             String name = bundle.getString("name");
     62             String age = bundle.getString("age");
     63             System.out.println(name+":"+age) ;
     64         } catch (Exception e) {
     65             e.printStackTrace();
     66         }
     67     }
     68     
     69     
     70     
     71     /**
     72      *4、 使用class变量的getResourceAsStream()方法
     73      */
     74     @Test
     75     public void read4(){
     76         try {
     77             InputStream stream = TestReadProperties.class.getResourceAsStream("/my.properties") ;
     78             Properties p=new Properties();
     79             p.load(stream);
     80             String name = p.get("name").toString() ;
     81             String age = p.get("age").toString() ;
     82             System.out.println(name+":"+age) ;
     83         } catch (Exception e) {
     84             e.printStackTrace();
     85         }
     86     }
     87     
     88     
     89     
     90     /**
     91      *5、 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
     92      */
     93     @Test
     94     public void read5(){
     95         try {
     96             InputStream stream = TestReadProperties.class.getClassLoader().getResourceAsStream("my.properties") ;
     97             Properties p=new Properties();
     98             p.load(stream);
     99             String name = p.get("name").toString() ;
    100             String age = p.get("age").toString() ;
    101             System.out.println(name+":"+age) ;
    102         } catch (Exception e) {
    103             e.printStackTrace();
    104         }
    105     }
    106     
    107     
    108     /**
    109      *6、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
    110      */
    111     @Test
    112     public void read6(){
    113         try {
    114             InputStream stream = ClassLoader.getSystemResourceAsStream("my.properties");
    115             Properties p=new Properties();
    116             p.load(stream);
    117             String name = p.get("name").toString() ;
    118             String age = p.get("age").toString() ;
    119             System.out.println(name+":"+age) ;
    120         } catch (Exception e) {
    121             e.printStackTrace();
    122         }
    123     }
    124     
    125 }

    配置文件:

         name的值是小明,这里配置文件会自动编码

    补充:

    Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
    示例:InputStream in = context.getResourceAsStream(path);
    Properties p = new Properties();
    p.load(in);

  • 相关阅读:
    my first android test
    VVVVVVVVVV
    my first android test
    my first android test
    my first android test
    ini文件
    ZZZZ
    Standard Exception Classes in Python 1.5
    Python Module of the Week Python Module of the Week
    my first android test
  • 原文地址:https://www.cnblogs.com/xujingyang/p/6857000.html
Copyright © 2011-2022 走看看