** 首發(fā)于www.dongxiaoxia.xyz **
一.getResourceAsStream
方法使用
**這里的getResourceAsStream
主要是針對(duì)Class
與ClassLoader
而言的 **
-
Class.getResourceAsStream(String path)
: path 不以’/'開(kāi)頭時(shí)默認(rèn)是從此類(lèi)所在的包下取資源,以’/'開(kāi)頭則是從ClassPath根下獲取闷供。其只是通過(guò)path構(gòu)造一個(gè)絕對(duì)路徑凹嘲,最終還是由ClassLoader獲取資源缀皱。 -
Class.getClassLoader.getResourceAsStream(String path)
:默認(rèn)則是從ClassPath根下獲取嫉嘀,path不能以’/'開(kāi)頭砾莱,最終是由ClassLoader獲取資源槽片。 -
ServletContext. getResourceAsStream(String path)
:默認(rèn)從WebAPP根目錄下取資源氏捞,Tomcat下path是否以’/'開(kāi)頭無(wú)所謂碧聪,當(dāng)然這和具體的容器實(shí)現(xiàn)有關(guān)。
獲取
Class
方法:
1.XX.class
2.this.getClass()
獲取ClassLoader
方法:
1.Thread.currentThread().getContextClassLoader()
2.Class.getClassLoader
**getResourceAsStream
用法大致有以下幾種: **
第一: 要加載的文件和.class文件在同一目錄下液茎,例如:com.x.y 下有類(lèi)me.class ,同時(shí)有資源文件myfile.xml
那么逞姿,應(yīng)該有如下代碼:
me.class.getResourceAsStream("myfile.xml");
第二:在me.class目錄的子目錄下,例如:com.x.y 下有類(lèi)me.class ,同時(shí)在 com.x.y.file 目錄下有資源文件myfile.xml
那么捆等,應(yīng)該有如下代碼:
me.class.getResourceAsStream("file/myfile.xml");
第三:不在me.class目錄下滞造,也不在子目錄下,例如:com.x.y 下有類(lèi)me.class ,同時(shí)在 com.x.file 目錄下有資源文件myfile.xml
那么栋烤,應(yīng)該有如下代碼:
me.class.getResourceAsStream("/com/x/file/myfile.xml");
總結(jié)一下谒养,可能只是兩種寫(xiě)法
第一:前面有 “ / ”
“ / ”代表了工程的根目錄,例如工程名叫做myproject明郭,“ / ”代表了myproject
me.class.getResourceAsStream("/com/x/file/myfile.xml");
第二:前面沒(méi)有 “ / ”
代表當(dāng)前類(lèi)的目錄
me.class.getResourceAsStream("myfile.xml");
me.class.getResourceAsStream("file/myfile.xml");
缺點(diǎn):
getResourceAsStream讀取的文件路徑只局限與工程的源文件夾中买窟,包括在工程src根目錄下,以及類(lèi)包里面任何位置薯定,但是如果配置文件路徑是在除了源文件夾之外的其他文件夾中時(shí)始绍,該方法是用不了的。
二.Properties
配置文件的讀取
1.使用Apache Commons Configuration
讀取配置信息
第三方標(biāo)準(zhǔn)jar包话侄,容易使用亏推,擁有眾多功能,不用重復(fù)造輪子,但是為了讀取一個(gè)配置文件就要引入一個(gè)jar包時(shí)就要考慮一下了年堆。
2.Spring ResourceLoader
Spring 能支持入?yún)⒙窂降暮芏喾绞酵毯迹ㄒ?" /"、"classpath" 開(kāi)頭变丧。
如果你想在項(xiàng)目中使用 Spring 提供的默認(rèn)配置文件載入實(shí)現(xiàn)芽狗,可以這樣書(shū)寫(xiě)你的代碼。
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource("log4j.properties");
Properties props = new Properties();
props.load(resource.getInputStream());
缺點(diǎn):依賴(lài)于Spring框架痒蓬,難以通用童擎, 如果用Spring框架應(yīng)該用Spring統(tǒng)一管理配置文件曼月,而不是當(dāng)作一個(gè)工具類(lèi)使用。
3.通過(guò) java.util.ResourceBundle
對(duì)象 操作
通過(guò)該方式僅能讀取配置文件而已柔昼,不能進(jìn)行寫(xiě)操作。示例:
// ResourceBundle rb = ResourceBundle.getBundle("配置文件相對(duì)工程根目錄的相對(duì)路徑(不含擴(kuò)展名)");
ResourceBundle rb = ResourceBundle.getBundle("config");
String name = rb.getString("name");
java自帶的類(lèi)炎辨,使用非常簡(jiǎn)單捕透,代碼量還少。但是功能略微簡(jiǎn)單碴萧。
4.通過(guò)Properties
對(duì)象手動(dòng)寫(xiě)個(gè)工具類(lèi)
通過(guò)Properties對(duì)象自定義封裝乙嘀,更能滿足自己需要的要求。
/**
* 根據(jù)Properties配置文件名稱(chēng)獲取配置文件對(duì)象
*
* @param propsFileName Properties配置文件名稱(chēng)(從ClassPath根下獲绕朴鳌)
* @return Properties對(duì)象
*/
private Properties getProperties(String propsFileName) {
if (propsFileName == null || propsFileName.equals("")) throw new IllegalArgumentException();
Properties properties = new Properties();
InputStream inputStream = null;
try {
try {
if (propsFileName.lastIndexOf(PROPERTY_FILE_SUFFIX) == -1) {//加入文件名后綴
propsFileName += PROPERTY_FILE_SUFFIX;
}
//寫(xiě)法1:
// inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(propsFileName);
//寫(xiě)法2:
// URL url = Thread.currentThread().getContextClassLoader().getResource(propsFileName);
// inputStream = url.openStream();
//寫(xiě)法3:
// inputStream = PropertiesLoader.class.getClassLoader().getResourceAsStream(propsFileName);
//寫(xiě)法4:
// URL url = PropertyUtil.class.getClassLoader().getResource(propsFileName);
// inputStream = url.openStream();
//寫(xiě)法5:
inputStream = PropertiesLoader.class.getResourceAsStream("/" + propsFileName);
if (null != inputStream) properties.load(inputStream);
} finally {
if (null != inputStream) {
inputStream.close();
}
}
} catch (IOException e) {
// LOGGER.error("加載屬性文件出錯(cuò)!", e);
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e);
}
return properties;
}
三.jar包讀取外部和內(nèi)部配置文件
1.獲取文件的方式
把項(xiàng)目打成jar包發(fā)布后jar中的文件就不能通過(guò)File file=new File("文件路徑")的方式來(lái)讀取文件了虎谢。
錯(cuò)誤方式:
URL url = DBHelper.class.getClassLoader().getResource(configName);
URL url = DBHelper.class.getResource("/db.properties");
從依賴(lài)的Jar包中讀取文件, Jar包內(nèi)的文件是無(wú)法用File讀取的,只能用Stream的方式讀取曹质。
正確方式:
InputStream inputStraean = DBHelper.class.getClassLoader().getResourceAsStream("db.properties")
結(jié)論:不能傳文件路徑婴噩,只能傳輸入流
2.測(cè)試獲取文件的各種情況
待測(cè)試jar包目錄:
測(cè)試目錄引用進(jìn)來(lái)的maven目錄

測(cè)試項(xiàng)目目錄
待測(cè)試jar包運(yùn)行結(jié)果:
測(cè)試項(xiàng)目運(yùn)行結(jié)果
根據(jù)測(cè)試得到的結(jié)果:
- 項(xiàng)目與項(xiàng)目引入jar包的文件都是在classpath下面的,也就是可以通過(guò)getResourceAsStream獲取羽德,jar包與項(xiàng)目的獲取方法沒(méi)有分別几莽,也就是說(shuō),獲取jar內(nèi)部與外部文件的方式是一樣的宅静。
- 如果項(xiàng)目與jar包在相對(duì)于classpath下有同樣的文件章蚣,則以項(xiàng)目的文件覆蓋jar包里面的文件。
- jar包里面沒(méi)有d.properties姨夹,所有jar包里面測(cè)試運(yùn)行沒(méi)有找到纤垂,但是到了項(xiàng)目里面,在項(xiàng)目下創(chuàng)建d.properties,可是可以發(fā)現(xiàn)該文件的磷账。
- 項(xiàng)目可以獲取項(xiàng)目的文件
項(xiàng)目可以獲取jar包里面的文件
jar包可以獲取項(xiàng)目的文件
jar包可以獲取jar包里面的文件
項(xiàng)目與jar包同時(shí)存在該文件峭沦,則以項(xiàng)目的文件優(yōu)先
因此,如果開(kāi)發(fā)一個(gè)庫(kù)或jar包够颠,可以保留一份默認(rèn)配置在jar包里面熙侍,也可以把配置文件放到客戶(hù)端配置,檢測(cè)時(shí)做處理履磨,如果客戶(hù)端配置該文件蛉抓,則以客戶(hù)端的配置為先,沒(méi)有找到就用默認(rèn)配置文件剃诅,或者別的處理(拋異常等巷送。。矛辕。)笑跛,slf4j配置我猜也是這樣子吧=_=付魔。
四.Properties配置文件讀取工具類(lèi)
package xyz.dongxiaoxia.commons.utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.logging.Logger;
/**
* Properties文件載入工具,可載入多個(gè)Properties文件飞蹂,相同屬性在最后載入的文件中的值將會(huì)覆蓋之前的值几苍,但以System的Property優(yōu)先。
*
* @author dongxiaoxia
* @create 2016-07-01 23:03
*/
public class PropertiesLoader {
private static Logger logger = Logger.getLogger(PropertiesLoader.class.getName());
/**
* .properties屬性文件名后綴
*/
public static final String PROPERTY_FILE_SUFFIX = ".properties";
private final Properties properties;
public PropertiesLoader(String... propsFileNames) {
properties = loadProperties(propsFileNames);
}
public PropertiesLoader(String propsFileName) {
properties = getProperties(propsFileName);
}
public PropertiesLoader(InputStream inputStream) {
properties = loadProperties(inputStream);
}
public Properties getProperties() {
return properties;
}
/**
* 取出Property陈哑,但與System的Property優(yōu)先妻坝,取不到返回空字符串。
*
* @param key
* @return
*/
private String getValue(String key) {
String systemProperty = System.getProperty(key);
if (systemProperty != null) {
return systemProperty;
}
if (properties.containsKey(key)) {
return properties.getProperty(key);
}
return "";
}
/**
* 取出String類(lèi)型的Property惊窖,但與System的Property優(yōu)先刽宪,如果都為NUll則拋出異常。
*
* @param key
* @return
*/
public String getProperty(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return value;
}
/**
* 取出String類(lèi)型的Property界酒,但以System的Property優(yōu)先圣拄,如果都為Null則返回Default值。
*
* @param key
* @param defaultValue
* @return
*/
public String getProperty(String key, String defaultValue) {
String value = getValue(key);
return value != null ? value : defaultValue;
}
/**
* 取出Integer類(lèi)型的Property毁欣。但以System的Property優(yōu)先庇谆,如果都為null或內(nèi)容錯(cuò)誤則拋異常。
*
* @param key
* @return
*/
public Integer getInteger(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Integer.valueOf(value);
}
/**
* 取出Integer類(lèi)型的Property署辉,但以System的Property優(yōu)先族铆。如果都為null則返回默認(rèn)值,如果內(nèi)容錯(cuò)誤則拋異常哭尝。
*
* @param key
* @param defaultValue
* @return
*/
public Integer getInteger(String key, Integer defaultValue) {
String value = getValue(key);
return value != null ? Integer.valueOf(value) : defaultValue;
}
/**
* 取出Double類(lèi)型的Property哥攘,但以System的Property優(yōu)先,如果都為null或內(nèi)容錯(cuò)誤則拋異常材鹦。
*
* @param key
* @return
*/
public Double getDouble(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Double.valueOf(value);
}
/**
* 取出Double類(lèi)型的Property逝淹,但以System的Property優(yōu)先,如果都為null則返回默認(rèn)值桶唐,如果內(nèi)容錯(cuò)誤則拋異常栅葡。
*
* @param key
* @param defaultValue
* @return
*/
public Double getDouble(String key, Double defaultValue) {
String value = getValue(key);
return value != null ? Double.valueOf(value) : defaultValue;
}
/**
* 取出Boolean類(lèi)型的Property,但以System的Property優(yōu)先尤泽,如果都為null則拋出異常欣簇,如果內(nèi)容不是true/false則返回false。
*
* @param key
* @return
*/
public Boolean getBoolean(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Boolean.valueOf(value);
}
/**
* 取出Boolean類(lèi)型的Property坯约,但以System的Property優(yōu)先熊咽,如果都為null則返回默認(rèn)值,如果內(nèi)容不為true/false則返回false闹丐。
*
* @param key
* @param defaultValue
* @return
*/
public Boolean getBoolean(String key, boolean defaultValue) {
String value = getValue(key);
return value != null ? Boolean.valueOf(value) : defaultValue;
}
public Set<Object> getAllKey() {
return properties.keySet();
}
public Collection<Object> getAllValues() {
return properties.values();
}
public Map<String, Object> getAllKeyValue() {
Map<String, Object> mapAll = new HashMap<>();
Set<Object> keys = getAllKey();
for (Object key1 : keys) {
String key = key1.toString();
mapAll.put(key, properties.get(key));
}
return mapAll;
}
/**
* 根據(jù)Properties配置文件名稱(chēng)獲取配置文件對(duì)象
*
* @param propsFileName Properties配置文件名稱(chēng)(從ClassPath根下獲群崤埂) 可以不帶擴(kuò)展名
* eg.根目錄下有個(gè)common.properties,那么可以傳“common”或者“common.properties”
* 根目錄下有個(gè)config文件夾,里面存在common.properties,那么可以傳“config/common”或者“config/common.properties”
* @return Properties對(duì)象
*/
private Properties getProperties(String propsFileName) {
if (propsFileName == null || propsFileName.equals("")) throw new IllegalArgumentException();
Properties properties = new Properties();
InputStream inputStream = null;
try {
try {
if (propsFileName.lastIndexOf(PROPERTY_FILE_SUFFIX) == -1) {//加入文件名后綴
propsFileName += PROPERTY_FILE_SUFFIX;
}
//寫(xiě)法1:
// inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(propsFileName);
//寫(xiě)法2:
// URL url = Thread.currentThread().getContextClassLoader().getResource(propsFileName);
// inputStream = url.openStream();
//寫(xiě)法3:
inputStream = PropertiesLoader.class.getClassLoader().getResourceAsStream(propsFileName);
//寫(xiě)法4:
// URL url = PropertyUtil.class.getClassLoader().getResource(propsFileName);
// inputStream = url.openStream();
//寫(xiě)法5:
// inputStream = PropertiesLoader.class.getResourceAsStream("/" + propsFileName);
if (null != inputStream) properties.load(inputStream);
} finally {
if (null != inputStream) {
inputStream.close();
}
}
} catch (IOException e) {
// LOGGER.error("加載屬性文件出錯(cuò)!", e);
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e);
}
return properties;
}
/**
* 載入多個(gè)文件
*
* @param propsFileNames
* @return
*/
private Properties loadProperties(String... propsFileNames) {
Properties pros = new Properties();
for (String propsFileName : propsFileNames) {
InputStream inputStream = null;
try {
inputStream = PropertiesLoader.class.getClassLoader().getResourceAsStream(propsFileName);
pros.load(inputStream);
} catch (Exception e) {
logger.info("Could not load properties from path:" + propsFileName + "," + e.getMessage());
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
logger.info(e.getMessage());
}
}
}
return pros;
}
/**
* 根據(jù)輸入流載入Properties對(duì)象
*
* @param inputStream
* @return
*/
private Properties loadProperties(InputStream inputStream) {
Properties pros = new Properties();
// pros.load(inputStream);
try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8")) {
pros.load(inputStreamReader);
} catch (Exception e) {
logger.info(e.getMessage());
}
return pros;
}
}
更多請(qǐng)查看GItHub PropertiesLoader類(lèi)