1. 程式人生 > >讀取配置檔案屬性資訊

讀取配置檔案屬性資訊

package com.facebac.yun.common.utils;

import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.Properties;

import org.springframework.core.io.support.PropertiesLoaderUtils;

/**  *   * @ClassName: TestProperties  * @author zhanghao  * @Description: 獲取配置檔案資訊  * @date: 2018年11月25日 上午10:56:00  * @version: 1.0.0  */ public class PropertiesUtil {

    /**      *       * @Title: printAllProperty      * @Description: 輸出所有配置資訊      * @param props      * @return void      * @throws      */     private static void printAllProperty(Properties props) {         @SuppressWarnings("rawtypes")         Enumeration en = props.propertyNames();         while (en.hasMoreElements()) {             String key = (String) en.nextElement();             String value = props.getProperty(key);             System.out.println(key + " : " + value);         }     }

    /**      * 根據key讀取value      *       * @Title: getProperties_1      * @Description: 第一種方式:根據檔名使用spring中的工具類進行解析 filePath是相對路勁,檔案需在classpath目錄下      *               比如:config.properties在包com.test.config下,      *               路徑就是com/test/config/config.properties      *       * @param filePath      * @param keyWord      * @return      * @return String      * @throws      */     public static String getProperties_1(String filePath, String keyWord) {         Properties prop = null;         String value = null;         try {             // 通過Spring中的PropertiesLoaderUtils工具類進行獲取             prop = PropertiesLoaderUtils.loadAllProperties(filePath);             // 根據關鍵字查詢相應的值             value = prop.getProperty(keyWord);         } catch (IOException e) {             e.printStackTrace();         }         return value;     }

    /**      * 讀取配置檔案所有資訊      *       * @Title: getProperties_1      * @Description: 第一種方式:根據檔名使用Spring中的工具類進行解析 filePath是相對路勁,檔案需在classpath目錄下      *               比如:config.properties在包com.test.config下,      *               路徑就是com/test/config/config.properties      *       * @param filePath      * @return void      * @throws      */     public static void getProperties_1(String filePath) {         Properties prop = null;         try {             // 通過Spring中的PropertiesLoaderUtils工具類進行獲取             prop = PropertiesLoaderUtils.loadAllProperties(filePath);             printAllProperty(prop);         } catch (IOException e) {             e.printStackTrace();         }     }

    /**      * 根據key讀取value      *       * @Title: getProperties_2      * @Description: 第二種方式:使用緩衝輸入流讀取配置檔案,然後將其載入,再按需操作 絕對路徑或相對路徑,      *               如果是相對路徑,則從當前專案下的目錄開始計算, 如:當前專案路徑/config/config.properties,      *               相對路徑就是config/config.properties      *       * @param filePath      * @param keyWord      * @return      * @return String      * @throws      */     public static String getProperties_2(String filePath, String keyWord) {         Properties prop = new Properties();         String value = null;         try {             // 通過輸入緩衝流進行讀取配置檔案             InputStream InputStream = new BufferedInputStream(                     new FileInputStream(new File(filePath)));             // 載入輸入流             prop.load(InputStream);             // 根據關鍵字獲取value值             value = prop.getProperty(keyWord);         } catch (Exception e) {             e.printStackTrace();         }         return value;     }

    /**      * 讀取配置檔案所有資訊      *       * @Title: getProperties_2      * @Description: 第二種方式:使用緩衝輸入流讀取配置檔案,然後將其載入,再按需操作 絕對路徑或相對路徑,      *               如果是相對路徑,則從當前專案下的目錄開始計算, 如:當前專案路徑/config/config.properties,      *               相對路徑就是config/config.properties      *       * @param filePath      * @return void      * @throws      */     public static void getProperties_2(String filePath) {         Properties prop = new Properties();         try {             // 通過輸入緩衝流進行讀取配置檔案             InputStream InputStream = new BufferedInputStream(                     new FileInputStream(new File(filePath)));             // 載入輸入流             prop.load(InputStream);             printAllProperty(prop);         } catch (Exception e) {             e.printStackTrace();         }     }

    /**      * 根據key讀取value      *       * @Title: getProperties_3      * @Description: 第三種方式: 相對路徑, properties檔案需在classpath目錄下,      *               比如:config.properties在包com.test.config下,      *               路徑就是/com/test/config/config.properties      * @param filePath      * @param keyWord      * @return      * @return String      * @throws      */     public static String getProperties_3(String filePath, String keyWord) {         Properties prop = new Properties();         String value = null;         try {             InputStream inputStream = PropertiesUtil.class                     .getResourceAsStream(filePath);             prop.load(inputStream);             value = prop.getProperty(keyWord);         } catch (IOException e) {             e.printStackTrace();         }         return value;     }

    /**      * 讀取配置檔案所有資訊      *       * @Title: getProperties_3      * @Description: 第三種方式: 相對路徑, properties檔案需在classpath目錄下,      *               比如:config.properties在包com.test.config下,      *               路徑就是/com/test/config/config.properties      * @param filePath      * @return      * @throws      */     public static void getProperties_3(String filePath) {         Properties prop = new Properties();         try {             InputStream inputStream = PropertiesUtil.class                     .getResourceAsStream(filePath);             prop.load(inputStream);             printAllProperty(prop);         } catch (IOException e) {             e.printStackTrace();         }     }

    public static void main(String[] args) {         // 注意路徑問題         String properties_1 = getProperties_1("resources/project.properties",                 "video.callback");         System.out.println("wechat_appid = " + properties_1);         // getProperties_1("resources/project.properties");         System.out.println("*********************************************");         /*          * // 注意路徑問題 String properties_2 =          * getProperties_2("resources/project.properties", "video_callback");          * System.out.println("jdbc.url = " + properties_2);          * getProperties_2("resources/project.properties");          * System.out.println("*********************************************");          * // 注意路徑問題 String properties_3 =          * getProperties_3("resources/project.properties", "video_callback");          * System.out.println("wechat_appid = " + properties_3);          * getProperties_3("resources/project.properties");          */     } }

package com.facebac.yun.test;

import service.Mw_EmailSend;

import com.facebac.yun.common.utils.PropertiesUtil;

public class PropertiesOperator {

    public static void main(String[] args) {         String emails = PropertiesUtil.getProperties_1(                 "resources/project.properties", "nvr.emails");         // String[] a = { "[email protected]" };         String[] a = emails.split("\\|");         for (int i = 0; i < a.length; i++) {             System.out.println(a[i]);         }

        // System.out.println(a);         Mw_EmailSend.sendEmails(a, "測試除錯通過配置檔案新增郵件收件人", "有為昨天Nvr請求下載資料統計");     }

}