Java加载资源文件的两种方法
作者:网络转载 发布时间:[ 2013/4/19 10:27:23 ] 推荐标签:
在使用Class和ClassLoader加载资源文件时,有几种区别细微的方法,修改ResourceLoader.java文件如下:
package davenkin;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ResourceLoader
{
public static void main(String[] args) throws IOException
{
ResourceLoader resourceLoader = new ResourceLoader();
resourceLoader.loadProperties1();
resourceLoader.loadProperties2();
resourceLoader.loadProperties3();
resourceLoader.loadProperties4();
resourceLoader.loadProperties5();
resourceLoader.loadProperties6();
}
public void loadProperties1() throws IOException
{
InputStream input = null;
try
{
input = Class.forName("davenkin.ResourceLoader").getResourceAsStream("/resources/config.properties");
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
printProperties(input);
}
public void loadProperties2() throws IOException
{
InputStream input = null;
input = this.getClass().getResourceAsStream("/resources/config.properties");
printProperties(input);
}
public void loadProperties3() throws IOException
{
InputStream input = this.getClass().getResourceAsStream("resources/config.properties");
printProperties(input);
}
public void loadProperties4() throws IOException
{
InputStream input = this.getClass().getClassLoader().getResourceAsStream("resources/config.properties");
printProperties(input);
}
public void loadProperties5() throws IOException
{
InputStream input = ClassLoader.getSystemResourceAsStream("resources/config.properties");
printProperties(input);
}
public void loadProperties6() throws IOException
{
InputStream input = ClassLoader.getSystemClassLoader().getResourceAsStream("resources/config.properties");
printProperties(input);
}
private void printProperties(InputStream input) throws IOException
{
Properties properties = new Properties();
properties.load(input);
System.out.println(properties.getProperty("name"));
}
}
以上程序输出结果为(请仔细揣摩,稍不小心(比如多加了一个"/"或少加了一个"/"),会报NullPointerException异常,表明你的资源文件没有找到):
ConfigUnderSrc
ConfigUnderSrc
ConfigUnderDavenkin
ConfigUnderSrc
ConfigUnderSrc
ConfigUnderSrc

sales@spasvo.com