Java中取资源时,经常用到Class.getResource和ClassLoader.getResource,这里来看看他们在取资源文件时候的路径问题。
获取方式有4种(类/类对象是否调取ClassLoader)
1.通过类.class.getResource()
2.通过类.class.getClassLoader().getResource()
3.通过类对象.getClass().getClassLoader()
4.通过类对象.getClass().getResource()
文件在工程中的位置这里介绍3种
1.src下
2.类所在目录下(同级目录下)
3.其他目录下
这样和上面的2中获取方式就组成了12种方法
类.class.getResource() | 类.class.getClassLoader().getResource() | 类对象.getClass().getClassLoader() | 类对象.getClass().getResource() | |
---|---|---|---|---|
src下 | ||||
同级目录下 | ||||
其他目录下 |
1.Class.getResource(String path)
path不以’/'开头时,默认是从此类所在的包下取资源;
path 以’/'开头时,则是从ClassPath根下获取;
System.out.println(JDBCUtil.class.getResource(""));
System.out.println(JDBCUtil.class.getResource("/"));
结果
2.classLoader().getResource()
path不能以’/'开头;
path是从ClassPath根下获取;
System.out.println(JDBCUtil.class.getClassLoader().getResource(""));
System.out.println(JDBCUtil.class.getClassLoader().getResource("/"));//null
结果
上述的12种方法获取资源方式:
package JDBC;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
public class JDBCUtil {
// 通过类.class.getClassLoader()的方式
//src下
public Properties getResSrcLoader() throws IOException {
Properties pro = new Properties();
URL res = JDBCUtil.class.getClassLoader().getResource("2.properties");
String path = res.getPath();
pro.load(new FileReader(path));
return pro;
}
// 通过类.class.getClassLoader()的方式
//同级目录下
public Properties getResFileLoader() throws IOException {
Properties pro = new Properties();
URL res = JDBCUtil.class.getClassLoader().getResource("JDBC/1.properties");
String path = res.getPath();
pro.load(new FileReader(path));
return pro;
}
// 通过类.class.getClassLoader()的方式
// 其他目录下
public Properties getResOtherFileLoader() throws IOException {
Properties pro = new Properties();
URL res = JDBCUtil.class.getClassLoader().getResource("day01/3.properties");
String path = res.getPath();
pro.load(new FileReader(path));
return pro;
}
// 通过类.class.getResource的方式
// src下
public Properties getResSrc() throws IOException {
Properties pro = new Properties();
URL res = JDBCUtil.class.getResource("/2.properties");
String path = res.getPath();
pro.load(new FileReader(path));
return pro;
}
// 通过类.class.getResource的方式
// 同级目录下
public Properties getResFile() throws IOException {
Properties pro = new Properties();
URL res = JDBCUtil.class.getResource("1.properties");
String path = res.getPath();
pro.load(new FileReader(path));
return pro;
}
//通过类.class.getResource的方式
// 其他目录下
public Properties getResOtherFile() throws IOException {
Properties pro = new Properties();
URL res = JDBCUtil.class.getResource("/day01/3.properties");
String path = res.getPath();
pro.load(new FileReader(path));
return pro;
}
// 通过对象.getClass().getClassLoader().getResource的方式
//src下
public Properties getResSrcLoader(JDBCUtil utils) throws IOException {
Properties pro = new Properties();
URL res = utils.getClass().getClassLoader().getResource("2.properties");
String path = res.getPath();
pro.load(new FileReader(path));
return pro;
}
// 通过对象.getClass().getClassLoader().getResource的方式
//同级目录下
public Properties getResFileLoader(JDBCUtil utils) throws IOException {
Properties pro = new Properties();
URL res = utils.getClass().getClassLoader().getResource("JDBC/1.properties");
String path = res.getPath();
pro.load(new FileReader(path));
return pro;
}
// 通过对象.getClass().getClassLoader().getResource的方式
// 其他目录下
public Properties getResOtherFileLoader(JDBCUtil utils) throws IOException {
Properties pro = new Properties();
URL res = utils.getClass().getClassLoader().getResource("day01/3.properties");
String path = res.getPath();
pro.load(new FileReader(path));
return pro;
}
// 通过对象.getClass().getResource的方式
// src下
public Properties getResSrc(JDBCUtil utils) throws IOException {
Properties pro = new Properties();
URL res = utils.getClass().getResource("/2.properties");
String path = res.getPath();
pro.load(new FileReader(path));
return pro;
}
// 通过类.class.getResource的方式
// 同级目录下
public Properties getResFile(JDBCUtil utils) throws IOException {
Properties pro = new Properties();
URL res = utils.getClass().getResource("1.properties");
String path = res.getPath();
pro.load(new FileReader(path));
return pro;
}
//通过类.class.getResource的方式
// 其他目录下
public Properties getResOtherFile(JDBCUtil utils) throws IOException {
Properties pro = new Properties();
URL res = utils.getClass().getResource("/day01/3.properties");
String path = res.getPath();
pro.load(new FileReader(path));
return pro;
}
public static void main(String[] args) throws IOException {
JDBCUtil utils = new JDBCUtil();
System.out.println(utils.getResSrcLoader());
System.out.println(utils.getResFileLoader());
System.out.println(utils.getResOtherFileLoader());
System.out.println(utils.getResSrc());
System.out.println(utils.getResFile());
System.out.println(utils.getResOtherFile());
System.out.println(utils.getResSrcLoader(utils));
System.out.println(utils.getResFileLoader(utils));
System.out.println(utils.getResOtherFileLoader(utils));
System.out.println(utils.getResSrc(utils));
System.out.println(utils.getResFile(utils));
System.out.println(utils.getResOtherFile(utils));
}
}
总结
getResource() 不加/表示当前目录下,加/表示当前项目根目录下
getClassLoader().getResource()不加/表示当前项目根目录下,加/为null
基于上述目录结构:
类.class.getResource() | 类.class.getClassLoader().getResource() | 类对象.getClass().getResource() | 类对象.getClass().getClassLoader() | |
---|---|---|---|---|
src下 | /文件 | 文件 | /文件 | 文件 |
同级目录下 | 文件 | 目录/文件 | 文件 | 目录/文件 |
其他目录下 | /其他目录/文件 | 其他目录/文件 | /其他目录/文件 | 其他目录/文件 |