1. 程式人生 > >配置檔案的讀取(Properties)

配置檔案的讀取(Properties)

package com.bootdo.common.config.collection.test.sort.properties;

import org.junit.Test;
import org.springframework.security.access.method.P;

import java.io.*;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * @ClassName :   Demo2
 * @Description: TODO
 * @Author: 13394
 * @CreateDate: 2018/10/28 12:40
 * @Version: 1.0
 */
public class Demo2 {
     /**
      * @Author zhangfu
      * @Description 寫檔案到對應的目錄(絕對路徑)
      * @Date 12:55 2018/10/28
      * @Param []
      * @return void
      */
    @Test
    public void test() throws IOException{
        //建立物件
        Properties pro =new Properties();
        //儲存
        pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
        pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
        pro.setProperty("user", "scott");
        pro.setProperty("pwd", "tiger");
        //獲取
        String url =pro.getProperty("url","test");
        System.out.println(url);
        //把檔案寫入到對應的資料夾中(絕對路徑)
        pro.store(new FileOutputStream(new File("e:/aa/db.properties")),"db");
        pro.storeToXML(new FileOutputStream(new File("e:/aa/db.xml")),"db");
    }
    /**
     * @Author zhangfu
     * @Description 寫對應的檔案(相對路徑)
     * @Date 12:56 2018/10/28
     * @Param []
     * @return void
     */
    @Test
    public void test3() throws FileNotFoundException,IOException{
        //建立物件
        Properties pro =new Properties();
        //儲存
        pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
        pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
        pro.setProperty("user", "scott");
        pro.setProperty("pwd", "tiger");
        //相對路徑當前的工程(預設)
        pro.store(new FileOutputStream(new File("src/main/java/com/bootdo/test/db.properties")),"db");
    }
    /**
     * @Author zhangfu
     * @Description 讀檔案(絕對路徑)
     * @Date 12:57 2018/10/28
     * @Param []
     * @return void
     */
    @Test
    public void test4() throws FileNotFoundException,IOException{
        Properties properties=new Properties();
        properties.load(new FileReader("E:/aa/db.properties"));
        properties.getProperty("url");
        System.out.println(properties.getProperty("url"));
    }
    /**
     * @Author zhangfu
     * @Description 讀配置檔案相對路徑
     * @Date 13:02 2018/10/28
     * @Param []
     * @return void
     */
    @Test
    public void test5() throws FileNotFoundException,IOException{
        Properties properties=new Properties();
        properties.load(new FileReader("src/main/java/com/bootdo/test/db.properties"));
        System.out.println(properties.getProperty("url"));
    }

    @Test
    public void test6() throws FileNotFoundException,IOException{
        Properties properties=new Properties();
        //bin 下面 有/
        properties.load(Demo2.class.getResourceAsStream("/test/java/com/bootdo/common/config/collection/test/sort/properties/db.properties"));
        //沒有/
        //properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("test/java/com/bootdo/common/config/collection/test/sort/properties/db.properties"));
        System.out.println(properties.getProperty("url"));
    }
}