第一章 屬性檔案操作工具類,第一章工具類

來源:互聯網
上載者:User

第一章 屬性檔案操作工具類,第一章工具類

1、代碼實現

給出的屬性檔案:http.properties(位於類路徑下)

1 #每個路由的最大串連數2 httpclient.max.conn.per.route = 20 3 #最大總串連數4 httpclient.max.conn.total = 4005 #連線逾時時間(ms)6 httpclient.max.conn.timeout = 10007 #操作逾時時間(ms)8 httpclient.max.socket.timeout = 1000View Code

注意:eclipse預設的屬性編輯器不可以顯示中文,安裝外掛程式(eclipse--propedit_5.3.3)即可。

屬性檔案操作工具:FileUtil

1 package com.util; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.Properties; 6 7 import org.apache.commons.lang.math.NumberUtils; 8 9 /**10 * 檔案操作工具類11 */12 public class FileUtil {13 14 /**15 * 載入屬性檔案*.properties16 * @param fileName 不是屬性全路徑名稱,而是相對於類路徑的名稱17 */18 public static Properties loadProps(String fileName){19 Properties props = null;20 InputStream is = null;21 22 try {23 is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);//擷取類路徑下的fileName檔案,並且轉化為輸入資料流24 if(is != null){25 props = new Properties();26 props.load(is); //載入屬性檔案27 }28 } catch (Exception e) {29 e.printStackTrace();30 }finally{31 if(is!=null){32 try {33 is.close();34 } catch (IOException e) {35 e.printStackTrace();36 }37 }38 }39 40 return props;41 }42 43 /*44 * 這裡只是列出了從屬性檔案中擷取int型資料的方法,擷取其他類型的方法相似45 */46 public static int getInt(Properties props, String key, int defaultValue){47 int value = defaultValue;48 49 if(props.containsKey(key)){ //屬性檔案中是否包含給定索引值50 value = NumberUtils.toInt(props.getProperty(key), defaultValue);//從屬性檔案中取出給定索引值的value,並且轉換為int型51 }52 53 return value;54 }55 56 /**57 * 測試58 */59 public static void main(String[] args) {60 Properties props = FileUtil.loadProps("http.properties");61 System.out.println(FileUtil.getInt(props, "httpclient.max.conn.per.route", 10));//屬性檔案中有這個key62 System.out.println(FileUtil.getInt(props, "httpclient.max.conn.per.route2", 10));//屬性檔案中沒有這個key63 }64 }View Code

注意:

  • 從類路徑下讀取檔案的方法Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
  • getInt()方法:使用了org.apache.commons.lang.math.NumberUtils類的toInt(String str,int defaultValue)方法,觀察原始碼 1 public static int toInt(String str, int defaultValue) { 2 if(str == null) { 3 return defaultValue; 4 } 5 try { 6 return Integer.parseInt(str); 7 } catch (NumberFormatException nfe) { 8 return defaultValue;//toInt("hello", 123) 9 }10 }View Code

    執行流程:當傳入的str為null時,直接返回defaultValue;否則,使用Integer.parseInt(str)將str轉換為int型,如果轉換成功(eg.str="123"),直接返迴轉換後的int型(eg.123),如果轉換不成功(eg.str="hello"),直接返回defaultValue。注意:這個工具類是值得借鑒的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.