JavaNote 4.7 讀取配置文件的多種方式

一、code

package com.test;


import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

public class Test {
    public static void main(String args[]){
        Test test = new Test();
        test.method1();
        test.method2();
        test.method3();
        test.method4();
        test.method5();
        test.method6();

    }
    public void method1(){
        Properties properties = new Properties();
        /*
        * InputStream inputStream = Test.class.getResourceAsStream("/db.properties");
        * InputStream inputStream1 = Test.class.getClassLoader().getResourceAsStream("db.properties");
        * 上面兩種方式可以達到一樣的效果*/
        InputStream inputStream = ClassLoader.getSystemResourceAsStream("db.properties");
        try{
            /*new Exception().getStackTrace()[0].getMethodName() 輸出本方法名,返回值String
            new Exception().getStackTrace()[0].getMethodName() 輸出調用者的方法名,返回值String
            * */
            properties.load(inputStream);
            System.out.println("methodName:" + new Exception().getStackTrace()[0].getMethodName() + " ; " + "root:" + " " +  properties.getProperty("root"));
        }
        catch(IOException e){
            e.getMessage();
        }
    }
    public void method2(){
        Properties properties = new Properties();
        try {
            //通過Spring的PropertiesLoaderUtils.loadAllProperties載入配置文件
            properties = PropertiesLoaderUtils.loadAllProperties("db.properties");
            System.out.println("methodName:" + new Exception().getStackTrace()[0].getMethodName() + " ; "+ "password:" + " " + properties.getProperty("password"));
        }
        catch (IOException e){
            e.getMessage();
        }
    }
    public void method3(){
        Properties properties = new Properties();
        try {
            //通過IO輸入流載入配置
            InputStream inStream = new FileInputStream(new File("src\\main\\resources\\db.properties"));
            properties.load(inStream);
            System.out.println("methodName:" + new Exception().getStackTrace()[0].getMethodName() + " ; "+ "root:" + " " + properties.getProperty("root"));
        }
        catch (IOException e){
            e.getMessage();
        }
    }
    public void method4(){
        Properties properties = new Properties();
        try {
            /*
            UrlResource urlResource = new UrlResource("file:target/classes/db.properties");
            InputStream inStream = urlResource.getInputStream();
            properties.load(inStream);
            System.out.println(properties.getProperty("password"));
*/
            //通過URL讀取配置文件,和上面的UrlResource達到的效果一致
            URL url = new URL("file:src\\main\\resources\\db.properties");
            InputStream inStream1 = url.openStream();
            properties.load(inStream1);
            System.out.println("methodName:" + new Exception().getStackTrace()[0].getMethodName() + " ; "+ "root:" + " " + properties.getProperty("root"));

        }
        catch (MalformedURLException e){
            e.getMessage();
        }
        catch (IOException e){
            e.getMessage();
        }
    }
    public void method5(){
        //通過java.util.ResourceBundle類加載配置文件,ResourceBundle.getBundle("db")中沒有使用配置文件後綴名
        ResourceBundle resource = ResourceBundle.getBundle("db");
        System.out.println("methodName:" + new Exception().getStackTrace()[0].getMethodName() + " ; "+ "root:" + " " +resource.getString("root") );
       //使用輸入流方式載入配置文件
        InputStream inputStream1 = Test.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            ResourceBundle resource1 = new PropertyResourceBundle(inputStream1);
            System.out.println("methodName:" + new Exception().getStackTrace()[0].getMethodName() + " ; "+ "password:" + " " +resource1.getString("password")+"2");
        }
        catch (IOException e){
            e.getMessage();
        }
    }
    public void method6(){
        Configurations configs = new Configurations();
        /*
        * 需要用到的commons-configuration2版本爲2.0及以上,還需要用到commons-beanutils依賴包
        */
        try {
            Configuration config = configs.properties(new File("db.properties"));
            System.out.print("methodName:" + new Exception().getStackTrace()[0].getMethodName() + " ; "+ "root:" + " " +config.getString("root") );

        }
        catch (ConfigurationException e){
            e.getMessage();
        }
    }
}

二、輸出截圖

三、第六種方法的依賴包

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-configuration2</artifactId>
  <version>2.3</version>
</dependency>
<dependency>
  <groupId>commons-beanutils</groupId>
  <artifactId>commons-beanutils</artifactId>
  <version>1.9.3</version>
</dependency>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章