java获取资源⽂件的⼏种⽅式
第⼀种采⽤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 = Class().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.Property("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完成查功能的,请参考。
在使⽤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 = Class().getResourceAsStream("/resources/config.properties");
printProperties(input);
}
public void loadProperties3() throws IOException
{
InputStream input = Class().getResourceAsStream("resources/config.properties");
printProperties(input);
}
public void loadProperties4() throws IOException
{
InputStream input = Class().getClassLoader().getResourceAsStream("resources/config.properties");
printProperties(input);
}
public void loadProperties5() throws IOException
{
InputStream input = SystemResourceAsStream("resources/config.properties");
printProperties(input);
}
public void loadProperties6() throws IOException
{
InputStream input = SystemClassLoader().getResourceAsStream("resources/config.properties");
printProperties(input);
}
private void printProperties(InputStream input) throws IOException
{
Properties properties = new Properties();
properties.load(input);
System.out.Property("name"));
}
}
第三种通过spring的Resoruce接⼝来加载资源也是最⽅便和最有效的⽅法:
boolean exists() 资源是否存在
boolean isOpen() 资源是否打开
URL getUEL() throws IOException如果底层资源可以表⽰成URL,则该⽅法返回URL对象。
FIle getFlie()  获取file对象
InputStream getInputStream() 获取输⼊流
Resoruce有许多实现类不同的实现类加载不同的⽂件
1.WritableResource接⼝实现类包括FileSystemResource和PathResource
java创建文件
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[] =Resources("classpath*:com/smart/**/*.xml");