1. java加載資源的方式
1.1 通過java i/o提供的接口以普通文件的形式加載
1.2 通過Class 類加載資源
public InputStream getResourceAsStream(String name);
如果使用Class類來加載資源文件需要區(qū)分以 '/' 開頭和不以 '/'開頭。
- 以 '/'開頭:Java以classpath為根目錄菱父,直接加上name來搜索資源文件革屠。
- 不以 '/'開頭:資源文件的全路徑應(yīng)該為:調(diào)用getResourceAsStream方法的類的package路徑加上name。
/**
* Add a package name prefix if the name is not absolute Remove leading "/"
* if name is absolute
*/
private String resolveName(String name) {
if (name == null) {
return name;
}
//不以 ‘/’開頭處理路徑,移除/,包名中.換成/
/**
* 比如:com.velocity --> com/velocity
**/
if (!name.startsWith("/")) {
Class<?> c = this;
while (c.isArray()) {
c = c.getComponentType();
}
String baseName = c.getName();
int index = baseName.lastIndexOf('.');
if (index != -1) {
name = baseName.substring(0, index).replace('.', '/')
+"/"+name;
}
} else {
//以 / 開頭緊緊簡單移除 /
name = name.substring(1);
}
return name;
}
1.3 通過ClassLoader 加載資源
public InputStream getResourceAsStream(String name);
用ClassLoader加載配置文件時宽涌,name均不能以"/"開頭,在查找時直接在classpath下進(jìn)行查找蝶棋。
1.4 java加載資源的demo
1.4.1 項目結(jié)構(gòu)
1.4.2 conf.properties
pro=pro
1.4.2 main函數(shù)
package com.velocity.resources;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @author mingxin
*/
public class ResourcesLoad {
public static void main(String[] args){
ResourcesLoad main = new ResourcesLoad();
try{
main.loadByClassLoader();
}catch (IOException e){
e.printStackTrace();
}
}
public void loadByClass() throws IOException{
InputStream input = this.getClass().getResourceAsStream("/conf.properties");
Properties properties = new Properties();
properties.load(input);
System.out.println(properties);
}
public void loadByClassLoader() throws IOException{
InputStream input = this.getClass().getClassLoader().getResourceAsStream("conf.properties");
Properties properties = new Properties();
properties.load(input);
System.out.println(properties);
}
}
2. velocity加載配置文件的方式
2.1 Velocity.init( String propsFilename )
找到 Velocity.init源碼
/**
* initialize the Velocity runtime engine, using default properties
* plus the properties in the properties file passed in as the arg
*
* @param propsFilename file containing properties to use to initialize
* the Velocity runtime
*/
public static void init( String propsFilename )
{
RuntimeSingleton.init(propsFilename);
}
一路找下去最終可以到ExtendedProperties,可以看到文件的加載是使用new File()來的卸亮,這樣我們必須給的是絕對路徑,所以init一般使用不帶參數(shù)版本或者Properties作為參數(shù)的。
/**
* Creates and loads the extended properties from the specified file.
*
* @param file the filename to load
* @param defaultFile a second filename to load default values from
* @throws IOException if a file error occurs
*/
public ExtendedProperties(String file, String defaultFile) throws IOException {
this.file = file;
basePath = new File(file).getAbsolutePath();
basePath = basePath.substring(0, basePath.lastIndexOf(fileSeparator) + 1);
FileInputStream in = null;
try {
in = new FileInputStream(file);
this.load(in);
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {}
}
if (defaultFile != null) {
defaults = new ExtendedProperties(defaultFile);
}
}