第一种采用class方式加载:
public InputStream getResourceAsStream(String pathToConfigFile);
举例:
举个例子,在IntelliJ Idea中创建一个java工程,目录结构如下:
该工程里有两个resources文件夹,一个位于davenkin文件夹下,一个直接位于src文件夹下。第一个resources文件夹下有一个config.properties文件,其内容为:
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(); } public void loadProperties1() throws IOException { InputStream input = null; try { input = Class.forName("davenkin.ResourceLoader").getResourceAsStream("/resources/config.properties"); //also can be this way: //input = this.getClass().getResourceAsStream("/resources/config.properties"); } catch (ClassNotFoundException e) { e.printStackTrace(); } printProperties(input); } private void printProperties(InputStream input) throws IOException { Properties properties = new Properties(); properties.load(input); System.out.println(properties.getProperty("name")); } }
输出结果为第二个resources文件夹下config.properties的内容:
ConfigUnderSrc
原因在于(请注意ReourceLoader.java文件中的红色部分):我们给出的资源文件路径(/resources/config.properties)以"/"开头,即使用的是绝对定位方式,所以找到的是直接在classpath下的resources文件夹。如果去掉资源文件文件路径前的"/",则采用的是相对定位方式,此时应该输出davenkin/resources/config.properties文件的内容。
第二种采用classLoader类加载资源文件
public InputStream getResourceAsStream(String pathToConfigFile);
用ClassLoader加载配置文件时,pathToConfigFile均不能以"/"开头,在查找时直接在classpath下进行查找。Class类在查找资源文件时,也是代理(delegate)给ClassLoader完成查找功能的,请参考Java官方文档。
在使用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")); } }
第三种通过spring的Resoruce接口来加载资源也是最方便和最有效的方法:
boolean exists() 资源是否存在
boolean isOpen() 资源是否打开
URL getUEL() throws IOException如果底层资源可以表示成URL,则该方法返回URL对象。
FIle getFlie() 获取file对象
InputStream getInputStream() 获取输入流
Resoruce有许多实现类不同的实现类加载不同的文件
1.WritableResource接口 实现类包括FileSystemResource和PathResource
2.ByteArrayResource 二进制数组表示资源
3.ClassPathResource:类路径下的资源,资源以相对于类路径的方式表示。
4.InputStreamResource:以输入流返回表示的资源
5.ServletContextResourse: 为访问Web容器的上下文的资源而设计的类以相对web应用根目录的路径加载资源
6.UrlResource:访问http资源和FTP资源。
7.PathResource:可以任意访问所有的资源。
资源加载器ResourceLoader:
getFile();
getURI()
getEesource();
支持ant风格
ResourcePatternResoler resolver=new PathMatchingResourcePatternResolver();
Resource resources[] =resolver.getResources("classpath*:com/smart/**/*.xml");