JAVA讀取classess下的.properties配置檔案封裝類,linux和windows上都可以使用
http://hi.baidu.com/myzhijie/item/3f73e988aef52bd85e0ec131import java.io.BufferedInputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 讀取Properties綜合類
* @author
*/
public class PropertiesUtil {
/**
* 配置檔案物件
*/
private Properties props=null;
/**
* 建構函式
* @param fileName 配置檔名稱
*/
public PropertiesUtil(String fileName){
String filePath=getPath(PropertiesUtil.class) + fileName;
props = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
props.load(in);
//關閉資源
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根據key值讀取配置的值
* Jun 26, 2010 9:15:43 PM
* @author 朱志傑
* @param key key值
* @return key 鍵對應的值
* @throws IOException
*/
public String readValue(String key) throws IOException {
return props.getProperty(key);
}
/**
* 讀取properties的全部資訊
* Jun 26, 2010 9:21:01 PM
* @author 朱志傑
* @throws FileNotFoundException 配置檔案沒有找到
* @throws IOException 關閉資原始檔,或者載入配置檔案錯誤
*
*/
public Map<String,String> readAllProperties() throws FileNotFoundException,IOException {
//儲存所有的鍵值
Map<String,String> map=new HashMap<String,String>();
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty(key);
map.put(key, Property);
}
return map;
}
/**
* 得到某一個類的路徑
* @param name
* @return
*/
private String getPath(Class name) {
String strResult = null;
if (System.getProperty("os.name").toLowerCase().indexOf("window") > -1) {
strResult = name.getResource("/").toString().replace("file:/", "")
.replace("%20", " ");
} else {
strResult = name.getResource("/").toString().replace("file:", "")
.replace("%20", " ");
}
return strResult;
}
}
相關文章
- java中讀取.properties配置檔案Java
- java讀取properties檔案Java
- Java讀取properties配置檔案工具包Java
- Java 對 properties 檔案操作 (ResourceBundle 類和 Properties 類)Java
- java Spring讀取properties檔案的注意點JavaSpring
- Java系列-如何讀取.properties屬性檔案Java
- Java讀取properties檔案連線資料庫Java資料庫
- Junit、Assert、內省、Properties類與配置檔案的使用
- java進階(36)--IO和Properties聯合使用(配置檔案)Java
- Linux下用C讀取INI配置檔案Linux
- mybatis讀取properties檔案內容MyBatis
- java中讀取配置檔案Java
- java Properties獲取本地檔案的變數Java變數
- Asp.net Core 和類庫讀取配置檔案資訊ASP.NET
- java讀取ApplicationResources.properties資原始檔JavaAPP
- Java讀取Json檔案工具類JavaJSON
- 【Java Web】——配置檔案的格式之xml和properties的區分JavaWebXML
- linux/windows 讀寫ini配置檔案LinuxWindows
- 讀取web應用下的資原始檔(例如properties)Web
- 好程式設計師Java培訓分享Java讀寫Properties配置檔案程式設計師Java
- Spring boot 讀取properties檔案的四種方式Spring Boot
- Go 專案配置檔案的定義和讀取Go
- 讀取resources中properties檔案內容範例
- Spring用程式碼來讀取properties檔案Spring
- java讀取resouces目錄下檔案Java
- IO相關的操作類封裝【檔案的讀取遍歷建立等等】(工具五)封裝
- 載入Java專案中scr下的properties檔案Java
- java 上傳 下載檔案工具類Java
- Xshell在Windows和Linux間檔案的上傳和下載WindowsLinux
- Java 讀取檔案Java
- Java使用commons-configuration讀取配置檔案Java
- viper 讀取配置檔案
- go配置檔案讀取Go
- java mysql儲存和讀取檔案JavaMySql
- spring配置檔案中分別使用多個properties檔案Spring
- [封裝] 小程式直連 oss 上傳檔案JS類封裝JS
- Java動態指令碼Groovy讀取配置檔案Java指令碼
- Java中的獲取檔案的物理絕對路徑,和讀取檔案Java