java讀取properties設定檔工具類

來源:互聯網
上載者:User

標籤:字串類型   prope   red   java讀取   buffere   load   user   turn   ==   

import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.InputStream;import java.util.Properties;/** * @功能描述: 擷取資源設定檔資訊 * * @zhangpj @建立時間:2015年12月3日 */public final class Globals { /** *global.properties 檔案 */ // 設定檔檔案名稱 private static String fileName = "GolbalsConfig"; // 設定檔尾碼名 private static String fileType = ".properties"; // 設定檔全名 private static String fullFileName = fileName+fileType; // 設定檔位置(相對路徑,WEB-INF目錄下) private static String relaPath = ""+fullFileName; // 設定檔內容 private static Properties pps = GetAllProperties(); /** * @功能描述: 擷取設定檔 * * @zhangpj @建立時間:2015年12月3日 * @return */ public static Properties GetAllProperties(){ // 項目根路徑 String rootPath =Globals.class.getResource("/").toString(); rootPath = rootPath.substring(rootPath.indexOf("/")+1); // 設定檔全路徑 String filePath = rootPath+relaPath; System.out.println("設定檔路徑"+filePath); Properties properties = new Properties(); try { InputStream in = new BufferedInputStream(new FileInputStream(filePath)); properties.load(in); } catch (Exception e) { System.err.println("擷取資源設定檔["+fullFileName+"]發生錯誤!"); } return properties; } /** * 通過key名擷取global.properties的值,字串類型 * * @param key * @return 返回字串 */ public static String getProp(String key) { return pps.getProperty(key); } /** * 通過key名擷取global.properties的值,整數類型 * * @param key * @return 返回字串 */ public static int getPropInt(String key) { String str = Globals.getProp(key); int rt = 0; try { rt = str == null ? 0 : Integer.parseInt(str.trim()); } catch (Exception e) { rt = 0; } return rt; } /** * 通過key名擷取global.properties的值,小數類型 * * @param key * @return 返回字串 */ public static float getPropFloat(String key) { String str = Globals.getProp(key); float rt = 0f; try { rt = str == null ? 0f : Float.parseFloat(str.trim()); } catch (Exception e) { rt = 0f; } return rt; } public static void main(String[] args) { System.out.println(Globals.getProp("ftpIp")); System.out.println(Globals.getPropInt("ftpPort")); System.out.println(Globals.getProp("ftpUserName")); System.out.println(Globals.getProp("ftpUserPass")); System.out.println(null == Globals.getProp("shopId222")); }}

java讀取properties設定檔工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.