1. 程式人生 > >第一章 屬性檔案操作工具類

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

1、程式碼實現

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

1 #每個路由的最大連線數
2 httpclient.max.conn.per.route = 20   
3 #最大總連線數
4 httpclient.max.conn.total = 400
5 #連線超時時間(ms)
6 httpclient.max.conn.timeout = 1000
7 #操作超時時間(ms)
8 httpclient.max.socket.timeout = 1000
View 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      * 載入屬性檔案*.properties
16 * @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));//屬性檔案中有這個key 62 System.out.println(FileUtil.getInt(props, "httpclient.max.conn.per.route2", 10));//屬性檔案中沒有這個key 63 } 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。注意:這個工具類是值得借鑑的。

相關推薦

第一 屬性檔案操作工具

1、程式碼實現 給出的屬性檔案:http.properties(位於類路徑下) 1 #每個路由的最大連線數 2 httpclient.max.conn.per.route = 20 3 #最大總連線數 4 httpclient.max.conn.total = 400 5 #連線超時

Access資料庫操作,Mdb檔案操作工具,UCanAccess使用

  Access資料庫操作,Mdb檔案操作工具類,UCanAccess使用     ================================ ©Copyright 蕃薯耀 2018年12月27日 http://fanshuyao.iteye.com/

Java檔案操作工具FileUtils

package com.suobei.xinzhiying.base.utils.file; import com.suobei.xinzhiying.base.result.ResponseMap; import com.suobei.xinzhiying.base.utils.aliy

檔案操作工具檔案/目錄的建立、刪除、移動、複製、zip壓縮與解壓.

FileOperationUtils.java package com.xnl.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import jav

檔案操作工具檔案/目錄的建立、刪除、移動、複製、zip壓縮與解壓

FileOperationUtils.java package com.xnl.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File;

Java的Excel檔案操作工具,包括讀、寫、合併等功能

一、直接上程式碼: package com.cycares.crm.utils.ExcelUtil; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputS

檔案操作工具 FileUtils常用方法

檔案操作工具類(FileUtils) 使用該工具類的前提是專案裡匯入commons-io 包 import org.apache.commons.io.FileUtils;  List<String> lines=new ArrayList<String&

Java讀取屬性檔案簡潔工具

屬性檔名稱UPay.properties,取鍵位UPay.AGENCY_RECHARGE_RET_URL的值: package com.onlinecredit.util; import java.io.IOException; import java.io.InputS

JAVA FileUtils(檔案讀寫以及操作工具)

轉別人的合併的連結 //https://www.cnblogs.com/chenhuan001/p/6575053.html   //https://blog.csdn.net/lovoo/article/details/77899627 第一個連結找不到了作者看見了說一下我給你上鍊接

使用javassist代替反射完成屬性操作工具

工作當中經常遇到需要使用反射進行成員變數遍歷的工作,但是使用反射的效率在高併發的情況下會比較低,這裡使用了javassit的方式生成了對應的處理類,通過其例項化物件完成遍歷工作  package com.ylink.ncpc.common.util; import jav

Unity中INI配置檔案操作工具

using UnityEngine; using System.IO; using System.Collections.Generic; /// <summary> /// unity中對ini配置檔案的操作 /// </summary> public class IniFile {

PreferencesUtils【SharedPreferences操作工具

ast 效果圖 void 垃圾清理 extends editor nac xtend git 版權聲明:本文為博主原創文章,未經博主允許不得轉載。 前言 可以替代ACache用來保存用戶名、密碼。 相較於Acache,不存在使用獵豹清理大師進行垃圾清理的時候把緩存的數

C#的access操作工具

C# access 操作工具類 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.OleDb; using System.Data

日期操作工具

ret catch pre pri 必須 led mem systemd stack 相關代碼如下 public class DateUtil { /** * * @param dateStyle 日期的樣式:如yyyyMMddHH

文件操作工具FileUtils

println 條件 == buffered ade mef cto override replace package yqw.java.util;import java.io.BufferedInputStream;import java.io.BufferedOutpu

常用判空操作工具

tis esc mail pre ati bst gradle otn ali 一、項目上校驗空和空集合地方非常多,可以編寫一個工具類統一校驗 package com.moy.custom.utils; import java.util.Collection; impo

c++對properties配置文件操作工具

c++ properties code源代碼GitHub路徑:源代碼地址下載 最近要使用c++對windows api相關接口的封裝,有2個接口要求讀寫properties文件。原以為網上應該有一大堆資料的,結果拜BAI度的大恩大德,一點相關的資料都沒有。那就只能自己動手豐衣足食。再次感謝十分強大只是找不到相

C#文件夾權限操作工具

輔助類 ace ont tex bject per right adding IT using System; using System.Collections.Generic; using System.IO; using System.Linq; using

JavaAes加密操作工具

ssi port castle 如果 ddp arrays array xfire instance package com.king.weixin.util;import java.io.UnsupportedEncodingException;import java.s

MySQL-讀取配置檔案工具與測試

package JDBCUtil; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.ut