1. 程式人生 > >java加載properties文件的六中基本方式實現

java加載properties文件的六中基本方式實現

alt user 通過 put main import port pac 技術

java加載properties文件的方式主要分為兩大類:一種是通過import java.util.Properties類中的load(InputStream in)方法加載;

另一種是通過import java.util.ResourceBundle類的getBundle(String baseName)方法加載。

註意:一定要區分路徑格式

實現代碼如下:

技術分享
  1 package com.util;
  2 
  3 import java.io.FileInputStream;
  4 import java.io.FileNotFoundException;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.util.Properties;
  8 import java.util.PropertyResourceBundle;
  9 import java.util.ResourceBundle;
 10 
 11 public class PropertiesUtil {
 12     private static String basePath = "src/prop.properties";
 13     private static String name = "";
 14     private static String nickname = "";
 15     private static String password = "";
 16 
 17     /**
 18      * 一、 使用java.util.Properties類的load(InputStream in)方法加載properties文件
 19      * 
 20      */
 21     public static String getName1() {
 22         try {
 23             Properties prop = new Properties();
 24             InputStream is = new FileInputStream(basePath);
 25             prop.load(is);
 26             name = prop.getProperty("username");
 27         } catch (FileNotFoundException e) {
 28             e.printStackTrace();
 29         } catch (IOException e) {
 30             e.printStackTrace();
 31         }
 32         return name;
 33     }
 34 
 35     /**
 36      * 二、 使用class變量的getResourceAsStream()方法
 37      * 註意:getResourceAsStream()讀取路徑是與本類的同一包下
 38      * 
 39      */
 40     public static String getName2() {
 41         Properties prop = new Properties();
 42         InputStream is = PropertiesUtil.class
 43                 .getResourceAsStream("/com/util/prop.properties");
 44         try {
 45             prop.load(is);
 46             name = prop.getProperty("username");
 47         } catch (IOException e) {
 48             e.printStackTrace();
 49         }
 50         return name;
 51     }
 52 
 53     /**
 54      * 三、
 55      * 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
 56      * getResourceAsStream(name)方法的參數必須是包路徑+文件名+.後綴 否則會報空指針異常
 57      * 
 58      */
 59     public static String getName3() {
 60         Properties prop = new Properties();
 61         InputStream is = PropertiesUtil.class.getClassLoader()
 62                 .getResourceAsStream("com/util/prop.properties");
 63         try {
 64             prop.load(is);
 65 
 66         } catch (IOException e) {
 67             e.printStackTrace();
 68         }
 69         return name;
 70     }
 71 
 72     /**
 73      * 四、 使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態方法
 74      * getSystemResourceAsStream()方法的參數格式也是有固定要求的
 75      * 
 76      */
 77     public static String getName4() {
 78         Properties prop = new Properties();
 79         InputStream is = ClassLoader
 80                 .getSystemResourceAsStream("com/util/prop.properties");
 81         try {
 82             prop.load(is);
 83             name = prop.getProperty("username");
 84         } catch (IOException e) {
 85             e.printStackTrace();
 86         }
 87         return name;
 88     }
 89 
 90     /**
 91      * 五、 使用java.util.ResourceBundle類的getBundle()方法
 92      * 註意:這個getBundle()方法的參數只能寫成包路徑+properties文件名,否則將拋異常
 93      * 
 94      */
 95     public static String getName5() {
 96         ResourceBundle rb = ResourceBundle.getBundle("com/util/prop");
 97         password = rb.getString("password");
 98         return password;
 99     }
100 
101     /**
102      * 六、 使用java.util.PropertyResourceBundle類的構造函數
103      * 
104      */
105     public static String getName6() {
106         try {
107             InputStream is = new FileInputStream(basePath);
108             ResourceBundle rb = new PropertyResourceBundle(is);
109             nickname = rb.getString("nickname");
110         } catch (FileNotFoundException e) {
111             e.printStackTrace();
112         } catch (IOException e) {
113             e.printStackTrace();
114         }
115 
116         return nickname;
117     }
118 
119     /**
120      * 測試
121      * 
122      */
123     public static void main(String[] args) {
124         System.out.println("name1:" + PropertiesUtil.getName1());
125         System.out.println("name2:" + PropertiesUtil.getName2());
126         System.out.println("name3:" + PropertiesUtil.getName3());
127         System.out.println("name4:" + PropertiesUtil.getName4());
128         System.out.println("password:" + PropertiesUtil.getName5());
129         System.out.println("nickname:" + PropertiesUtil.getName6());
130     }
131 }
技術分享

文件路徑:

技術分享

prop.properties文件:

1 username=mamama
2 nickname=xiaoma
3 password=123456

輸出結果:

技術分享

java加載properties文件的六中基本方式實現