classpath概念:
類路徑:也就是可執(zhí)行文件.class文件所在路徑
java項(xiàng)目中:%項(xiàng)目%/bin下
javaweb項(xiàng)目中:%項(xiàng)目%/WEB-INF/classes下
Class.getResource(""):
Class.getResource("/"):
java項(xiàng)目中:
public static void main(String[] args) {
URL url = Test.class.getResource("");
System.out.println("路徑:"+url); //在classpath/類所在包下
url = Test.class.getResource("/");
System.out.println("路徑/:"+url); //在classpath下
}
路徑:file:/D:/workspace/tmp/bin/com/xxjqr/tmp/test/
路徑/:file:/D:/workspace/tmp/bin/
web項(xiàng)目中:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
URL url = DownloadServlet.class.getResource("");
System.out.println("路徑:"+url); //在classpath/類所在包下
url = DownloadServlet.class.getResource("/");
System.out.println("路徑/:"+url); //在classpath下
}
路徑:file:/D:/tomcat/apache-tomcat-7.0.70/webapps/jdbc_study/WEB-INF/classes/com/xxjqr/jdbc_study/web/
路徑/:file:/D:/tomcat/apache-tomcat-7.0.70/webapps/jdbc_study/WEB-INF/classes/
Class.getClassLoader().getResource(""):
Class.getClassLoader().getResource("/"):
java項(xiàng)目中
public static void main(String[] args) {
URL url = Test.class.getClassLoader().getResource("");
System.out.println("路徑:"+url); //在classpath/類所在包下
url = Test.class.getClassLoader().getResource("/");
System.out.println("路徑/:"+url); //null
}
路徑:file:/D:/workspace/tmp/bin/
路徑/:null
web項(xiàng)目中
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
URL url = DownloadServlet.class.getClassLoader().getResource("");
System.out.println("路徑:"+url); //classpath下
url = DownloadServlet.class.getClassLoader().getResource("/");
System.out.println("路徑/:"+url); //classpath下
}
路徑:file:/D:/tomcat/apache-tomcat-7.0.70/webapps/jdbc_study/WEB-INF/classes/
路徑/:file:/D:/tomcat/apache-tomcat-7.0.70/webapps/jdbc_study/WEB-INF/classes/
Thread.currentThread().getContextClassLoader().getResource("");
Thread.currentThread().getContextClassLoader().getResource("/");
web項(xiàng)目中
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
URL url = Thread.currentThread().getContextClassLoader().getResource("");
System.out.println("路徑:"+url); //classpath下
url = Thread.currentThread().getContextClassLoader().getResource("");
System.out.println("路徑/:"+url); //classpath下
}
路徑:file:/D:/tomcat/apache-tomcat-7.0.70/webapps/jdbc_study/WEB-INF/classes/
路徑/:file:/D:/tomcat/apache-tomcat-7.0.70/webapps/jdbc_study/WEB-INF/classes/
getServletContext().getRealPath(""):
getServletContext().getRealPath("/"):
web項(xiàng)目中
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = getServletContext().getRealPath("");
System.out.println("路徑:"+path); //項(xiàng)目根目錄下
path = getServletContext().getRealPath("/");
System.out.println("路徑/:"+path); //項(xiàng)目根目錄下
}
路徑:D:\tomcat\apache-tomcat-7.0.70\webapps\jdbc_study
路徑/:D:\tomcat\apache-tomcat-7.0.70\webapps\jdbc_study\
getServletContext().getResource(""):
getServletContext().getResource("/"):
web項(xiàng)目中
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
URL url = getServletContext().getResource("");
System.out.println("路徑:"+url); //項(xiàng)目根目錄下
url = getServletContext().getResource("/");
System.out.println("路徑/:"+url); //項(xiàng)目根目錄下
}
路徑:jndi:/localhost/jdbc_study/
路徑/:jndi:/localhost/jdbc_study/
補(bǔ)充:
.:是相對(duì)路徑,表示當(dāng)前項(xiàng)目呐籽,只能在java項(xiàng)目中使用
獲取資源舉例:
java項(xiàng)目中獲取資源文件:
java項(xiàng)目中scr下的資源文件分別在src,bin目錄下都有一份,所以可以通過(guò)如下方法來(lái)獲取
public class Test2 {
public static void main(String[] args) throws IOException {
//--------獲取src下文件-----
//方式1:
// InputStream in = new FileInputStream(new File("./bin/user.properties"));
// InputStream in = new FileInputStream(new File("./src/user.properties"));
//方式2:
// Class clazz = Test2.class;
// InputStream in= clazz.getResourceAsStream("/user.properties");//bin目錄下的資源文件
//方式3:
// InputStream in = Test2.class.getClassLoader().getResourceAsStream("user.properties");
// Properties properties = new Properties();
// properties.load(in);
// String name = properties.getProperty("name");
// String password = properties.getProperty("password");
// System.out.println(name+":"+password);
//--------獲取file目錄下文件-----
//方式1:
// InputStream in = new FileInputStream(new File("./src/file/喬布斯.txt"));
//方式2:
// Class clazz = Test2.class;
// InputStream in= clazz.getResourceAsStream("/file/喬布斯.txt");//bin目錄下的資源文件
//方式3:
InputStream in = Test2.class.getClassLoader().getResourceAsStream("file/喬布斯.txt");
byte buffer[] = new byte[1024];
int length = 0;
while((length = in.read(buffer))!=-1){
System.out.println(new String(buffer,0,length));
}
}
}
web項(xiàng)目中獲取資源文件:
查看項(xiàng)目的配置就可以知道web項(xiàng)目中src下的內(nèi)容會(huì)被組裝到WEB/INF/classes下;而classes又正是web項(xiàng)目的執(zhí)行類根目錄冀惭;所以可以通過(guò)如下方法來(lái)獲取
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//----------讀取src下文件---------
//方式1:
// InputStream in = DownloadServlet.class.getResourceAsStream("/user.properties");
//方式2:
// InputStream in = DownloadServlet.class.getClassLoader().getResourceAsStream("user.properties");
//方式3:
// InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("user.properties");
//方式4:
// InputStream in = getServletContext().getResourceAsStream("WEB-INF/classes/user.properties");
// String path = getServletContext().getRealPath("WEB-INF/classes/user.properties");
// InputStream in = new FileInputStream(new File(path));
// Properties properties = new Properties();
// properties.load(in);
// String name = properties.getProperty("name");
// String password = properties.getProperty("password");
// System.out.println(name+":"+password);
//----------讀取WEB-INF/下目錄下文件--------
//方式1:
// InputStream in = getServletContext().getResourceAsStream("WEB-INF/files/喬布斯.txt");
//方式2:
String path = getServletContext().getRealPath("WEB-INF/files/喬布斯.txt");
InputStream in = new FileInputStream(new File(path));
byte buffer[] = new byte[1024];
int length = 0;
while((length = in.read(buffer))!=-1){
System.out.println(new String(buffer,0,length));
}
}
疑問(wèn):
這么多方法都可以獲取資源躏碳,那么挑選哪個(gè)方法使用呢金砍?
在可用的情況下,盡量使用(原因可能是線程安全問(wèn)題)
Thread.currentThread().getContextClassLoader().getResource("");
Thread.currentThread().getContextClassLoader().getResource("/");