1. 程式人生 > >java載入properties檔案的幾種方式

java載入properties檔案的幾種方式

package com;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

public class LoadPropertiesFile {
private static final String DEFAULT_ENCODING = "UTF-8";
    
    /**
     * 使用java.util.Properties類的load()方法載入properties檔案
     */
    private static void Method1() {
        try {
            // 獲取檔案流(方法1或2均可)
            InputStream inputStream = new BufferedInputStream(
            		new FileInputStream(
            				new File("src/main/resources/demo/jdbc.properties")
            				)); //方法1
//            InputStream inputStream = Thread.currentThread()
//            							.getContextClassLoader()
//            							.getResourceAsStream("jdbc.properties"); //方法2
            Properties prop = new Properties();
            
            prop.load(new InputStreamReader(inputStream, DEFAULT_ENCODING)); //載入格式化後的流
            
            String driverClassName = prop.getProperty("driverClassName");
            
            System.out.println("Method1: " + driverClassName);
            
        } catch (FileNotFoundException e) {
            System.out.println("properties檔案路徑有誤!");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 使用class變數的getResourceAsStream()方法
     * 注意:getResourceAsStream()方法的引數路徑/包路徑+properties檔名+.字尾
     */
    public static void Method2() {
        try {
            InputStream inputStream = LoadPropertiesFile.class.getResourceAsStream("/demo/jdbc.properties");
            
            Properties prop = new Properties();
            prop.load(inputStream);
            
            String driverClassName = prop.getProperty("driverClassName");
            
            System.out.println("Method2: " + driverClassName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 
     * 注意:getResourceAsStream(name)方法的引數必須是包路徑+檔名+.字尾
     */
    public static void Method3() {
        try {
            InputStream inputStream = LoadPropertiesFile.class.getClassLoader().getResourceAsStream("demo/jdbc.properties");
            
            Properties prop = new Properties();
            prop.load(inputStream);
            
            String driverClassName = prop.getProperty("driverClassName");
            
            System.out.println("Method3: " + driverClassName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態方法
     * getSystemResourceAsStream()方法的引數必須是包路徑+檔名+.字尾
     */
    public static void Method4() {
        try {
            InputStream inputStream = ClassLoader.getSystemResourceAsStream("demo/jdbc.properties");
            
            Properties prop = new Properties();
            prop.load(inputStream);
            
            String driverClassName = prop.getProperty("driverClassName");
            
            System.out.println("Method4: " + driverClassName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 使用java.util.ResourceBundle類的getBundle()方法
     * 注意:注意:這個getBundle()方法的引數相對同目錄路徑,並去掉.properties字尾,否則將拋異常
     */
    public static void Method5() {
        ResourceBundle resource = ResourceBundle.getBundle("demo");
        String driverClassName = resource.getString("driverClassName");
        
        System.out.println("Method5: " + driverClassName);
    }
    
    /**
     * 使用java.util.PropertyResourceBundle類的建構函式
     */
    public static void Method6(){
        ResourceBundle resource;
        try {
            InputStream inputStream = new BufferedInputStream(new FileInputStream(new File("src/main/resources/demo/jdbc.properties")));
            resource = new PropertyResourceBundle(inputStream);
            
            String driverClassName = resource.getString("driverClassName");
            System.out.println("Method6: " + driverClassName);
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        Method1();
        Method2();
        Method3();
        Method4();
        Method5();
        Method6();
    }
}

5,6是通過ResourceBundle類來載入Properties檔案,然後ResourceBundle物件來操做properties檔案內容;

其中最重要的就是每種方式載入檔案時,檔案的路徑一定要對。

 

properties檔案以及專案結:

1

2

3

4

5

#\u73af\u4fdd\u5927\u68c0\u67e5\u6570\u636e\u5e93\u6570\u636e\u5e93\u76f8\u5173

driverClassName=com.mysql.jdbc.Driver

url=jdbc\:mysql\://localhost\:3306/mydatabase?useUnicode\=true&characterEncoding\=utf-8&generateSimpleParameterMetadata\=true

username=root

password=test123

 

1:帶類載入器:

this.getClass().getClassLoader().getResourceAsStream("testVariables.bpmn") 從classpath根目錄下載入指定名稱的檔案 

this.getClass().getResourceAsStream("testVariables.bpmn") 從當前包下載入指定名稱的檔案

 this.getClass().getResourceAsStream("/testVariables.bpmn") 從classpath根目錄下載入指定名稱的檔案