Java Web讀取properties配置檔案
阿新 • • 發佈:2019-01-10
java action讀取src目錄下的properties配置檔案。
mailServer.properties配置檔案如下:
mailServerHost = smtp.163.com
mailServerPort = 25
authValidate = true
userName = [email protected]
讀取配置檔案類GetProperty程式碼如下:
package com.hsinghsu.test.action; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class GetProperty { // 方法一:通過java.util.ResourceBundle讀取資源屬性檔案 public static String getPropertyByName(String path, String name) { String result = ""; try { // 方法一:通過java.util.ResourceBundle讀取資源屬性檔案 result = java.util.ResourceBundle.getBundle(path).getString(name); System.out.println("name:" + result); } catch (Exception e) { System.out.println("getPropertyByName2 error:" + name); } return result; } // 方法二:通過類載入目錄getClassLoader()載入屬性檔案 public static String getPropertyByName2(String path, String name) { String result = ""; // 方法二:通過類載入目錄getClassLoader()載入屬性檔案 InputStream in = GetProperty.class.getClassLoader() .getResourceAsStream(path); // InputStream in = // this.getClass().getClassLoader().getResourceAsStream("mailServer.properties"); // 注:Object.class.getResourceAsStream在action中呼叫報錯,在普通java工程中可用 // InputStream in = // Object.class.getResourceAsStream("/mailServer.properties"); Properties prop = new Properties(); try { prop.load(in); result = prop.getProperty(name).trim(); System.out.println("name:" + result); } catch (IOException e) { System.out.println("讀取配置檔案出錯"); e.printStackTrace(); } return result; } }
action程式碼如下:
呼叫action,即可獲取相應配置檔案的屬性值。package com.hsinghsu.test.action; import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport { private static final long serialVersionUID = 3348881101306356364L; public String test(){ System.out.println("=="+GetProperty.getPropertyByName("mailServer","userName")); System.out.println("==>>"+GetProperty.getPropertyByName2("mailServer.properties","userName")); // //以下引數從properties檔案讀取 // String mailServerHost = null; // 傳送郵件的伺服器的IP // String mailServerPort = null; // 傳送郵件的伺服器埠 // String userName = null; // 登陸郵件傳送伺服器的使用者名稱 // boolean authValidate = false; // 是否需要身份驗證 // // try { // //方法一:通過java.util.ResourceBundle讀取資源屬性檔案 // mailServerHost = java.util.ResourceBundle.getBundle("mailServer").getString("mailServerHost"); // System.out.println("mailServerHost:"+mailServerHost); // } catch (Exception e) { // System.out.println("mailServerHost error:"+mailServerHost); // } // // //方法二:通過類載入目錄getClassLoader()載入屬性檔案 //// InputStream in = TestAction.class.getClassLoader().getResourceAsStream("mailServer.properties"); // InputStream in = this.getClass().getClassLoader().getResourceAsStream("mailServer.properties"); // // //注:Object.class.getResourceAsStream在action中呼叫報錯,在普通java工程中可用 //// InputStream in = Object.class.getResourceAsStream("/mailServer.properties"); // Properties prop = new Properties(); // try { // prop.load(in); // mailServerHost = prop.getProperty("mailServerHost").trim(); // mailServerPort = prop.getProperty("mailServerPort").trim(); // userName = prop.getProperty("userName").trim(); // authValidate = prop.getProperty("authValidate").trim().equalsIgnoreCase("true"); // // System.out.println("mailServerHost:"+mailServerHost+" mailServerPort:"+mailServerPort+" userName:"+userName+" authValidate:"+authValidate); // } catch (IOException e) { // System.out.println("讀取郵箱服務配置檔案出錯"); // e.printStackTrace(); // } // return null; } }