用JAVA获取文件,听似简单,但对于很多像我这样的新人来说,还是掌握颇浅,用起来感觉颇深,大常最经常用的,就是用JAVA的File类,如要取得 c:/test.txt文件,就会这样用File file = new File("c:/test.txt");这样用有什么问题,相信大家都知道,就是路径硬编码,对于JAVA精神来说,应用应该一次成型,到处可用,并且从现实应用来讲,最终生成的应用也会部署到Windows外的操作系统中,对于linux来说,在应用中用了c:/这样的字样,就是失败,所以,我们应该尽量避免使用硬编码,即直接使用绝对路径。
在Servlet应用中,有一个getRealPath(String str)的方法,这个方法尽管也可以动态地获得文件的路径,不秘直接手写绝对路径,但这也是一个不被建议使用的方法,那么,我们有什么方法可以更好地获得文件呢?
那就是Class.getResource()与Class.getResourceAsStream()方法,但很多人还是不太懂它的用法,因为很多人(比如不久前的我)都不知道应该传怎么样的参数给它,当然,有些人己经用得如火纯青,这些人是不需要照顾的,在此仅给不会或者还不是很熟的人解释一点点。
比如我们有以下目录
|--project
|--src
|--javaapplication
|--Test.java
|--file1.txt
|--file2.txt
|--build
|--javaapplication
|--Test.class
|--file3.txt
|--file4.txt
在上面的目录中,有一个src目录,这是JAVA源文件的目录,有一个build目录,这是JAVA编译后文件(.class文件等)的存放目录
那么,我们在Test类中应该如何分别获得
file1.txt file2.txt file3.txt file4.txt这四个文件呢?
首先讲file3.txt与file4.txt
file3.txt:
- 方法一:File file3 = new File(Test.class.getResource("file3.txt").getFile());
- 方法二:File file3 = new File(Test.class.getResource("/javaapplication/file3.txt").getFile());
- 方法三:File file3 = new File(Test.class.getClassLoader().getResource("javaapplication/file3.txt").getFile());
方法一:File file3 = new File(Test.class.getResource("file3.txt").getFile());
方法二:File file3 = new File(Test.class.getResource("/javaapplication/file3.txt").getFile());
方法三:File file3 = new File(Test.class.getClassLoader().getResource("javaapplication/file3.txt").getFile());
file4.txt:
- 方法一:File file4 = new File(Test.class.getResource("/file4.txt").getFile());
- 方法二:File file4 = new File(Test.class.getClassLoader().getResource("file4.txt").getFile());
方法一:File file4 = new File(Test.class.getResource("/file4.txt").getFile());
方法二:File file4 = new File(Test.class.getClassLoader().getResource("file4.txt").getFile());
很好,我们可以有多种方法选择,但是file1与file2文件呢?如何获得?
答案是,你只能写上它们的绝对路径,不能像file3与file4一样用class.getResource()这种方法获得,它们的获取方法如下
假如整个project目录放在c:/下,那么file1与file2的获取方法分别为
file1.txt
- 方法一:File file1 = new File("c:/project/src/javaapplication/file1.txt");
- 方法二:。。。没有
方法一:File file1 = new File("c:/project/src/javaapplication/file1.txt");
方法二:。。。没有
file2.txt
- 方法一:File file2 = new File("c:/project/src/file2.txt");
- 方法二:。。。也没有
方法一:File file2 = new File("c:/project/src/file2.txt");
方法二:。。。也没有
总结一下,就是你想获得文件,你得从最终生成的.class文件为着手点,不要以.java文件的路径为出发点,因为真正使用的就是.class,不会拿个.java文件就使用,因为java是编译型语言嘛
至于getResouce()方法的参数,你以class为出发点,再结合相对路径的概念,就可以准确地定位资源文件了,至于它的根目录嘛,你用不同的IDE build出来是不同的位置下的,不过都是以顶层package作为根目录,比如在Web应用中,有一个WEB-INF的目录,WEB-INF目录里面除了web.xml文件外,还有一个classes目录,没错了,它就是你这个WEB应用的package的顶层目录,也是所有.class的根目录 “/”,假如clasaes目录下面有一个file.txt文件,它的相对路径就是"/file.txt",如果相对路径不是以"/"开头,那么它就是相对于.class的路径。。
还有一个getResourceAsStream()方法,参数是与getResouce()方法是一样的,它相当于你用getResource()取得File文件后,再new InputStream(file)一样的结果
来源:http://gavin-chen.javaeye.com/blog/261151
public URL getResource(String name)
-
查找带有给定名称的资源,查找给定类相关的资源的规则是通过定义类的
class loader 实现的。此方法委托给此对象的类加载器。如果此对象通过引导类加载器加载,则此方法将委托给
ClassLoader.getSystemResource(java.lang.String)
。在委托前,使用下面的算法从给定的资源名构造一个绝对资源名:
- 如果 name 以 '/' 开始,则绝对资源名是 '/' 后面的 name 的一部分。
- 否则,绝对名具有以下形式:
modified_package_name/name
其中 modified_package_name 是此对象的包名,该名用 '/' 取代了 '.' ('/u002e')。
- Class.getResource(""); 获取classpath
- Class.getResource("JMF.class"); 代表获取相于类路径当前包下的SendService.class的类路径.
- /D:/bak/upload/upload/WebRoot/WEB-INF/classes/jmf/JMF.class-------->打印出的结果
- Class.getResource("/jmf/WebCamSwing.class"); /jmf/WebCamSwing.class->代表相于类路径的绝对路径
- file:/D:/bak/upload/upload/WebRoot/WEB-INF/classes/jmf/JMF.class -------->打印出的结果
Class.getResource(""); 获取classpath
Class.getResource("JMF.class"); 代表获取相于类路径当前包下的SendService.class的类路径.
/D:/bak/upload/upload/WebRoot/WEB-INF/classes/jmf/JMF.class-------->打印出的结果
Class.getResource("/jmf/WebCamSwing.class"); /jmf/WebCamSwing.class->代表相于类路径的绝对路径
file:/D:/bak/upload/upload/WebRoot/WEB-INF/classes/jmf/JMF.class -------->打印出的结果
我们怎么获得Object的类路径:
Class.getResource("/java/lang/Object.class") 因为Object是通过引导类加载器 (BootStrapClassLoader)加载的,所以此方法通过系统类加载器来查找资料, 所以我们要指定类的绝对路径/java /lang/Object.class
- public java.net.URL getResource(String name) {
- name = resolveName(name);
- ClassLoader cl = getClassLoader0();
- if (cl==null) {
- // A system class.
- return ClassLoader.getSystemResource(name);
- }
- return cl.getResource(name);
- }
public java.net.URL getResource(String name) {
name = resolveName(name);
ClassLoader cl = getClassLoader0();
if (cl==null) {
// A system class.
return ClassLoader.getSystemResource(name);
}
return cl.getResource(name);
}
我们来看看如何通过系统类加载器来查找Object:
- Class.getClassLoader().getSystemResource("java/lang/Object.class")
Class.getClassLoader().getSystemResource("java/lang/Object.class")
打印出来的结果多是:
jar:file:/E:/Program/Java/jdk1.5.0_15/jre/lib/rt.jar!/java/lang/Object.class
为什么getResource("")前面要加"/",而getSystemResource("")前面不用加呢?
- private String resolveName(String name) {
- if (name == null) {
- return name;
- }
- if (!name.startsWith("/")) {
- Class c = this;
- while (c.isArray()) {
- c = c.getComponentType();
- }
- String baseName = c.getName();
- int index = baseName.lastIndexOf('.');
- if (index != -1) {
- name = baseName.substring(0, index).replace('.', '/')
- +"/"+name;
- }
- } else {
- name = name.substring(1);
- }
- return name;
- }
private String resolveName(String name) {
if (name == null) {
return name;
}
if (!name.startsWith("/")) {
Class c = this;
while (c.isArray()) {
c = c.getComponentType();
}
String baseName = c.getName();
int index = baseName.lastIndexOf('.');
if (index != -1) {
name = baseName.substring(0, index).replace('.', '/')
+"/"+name;
}
} else {
name = name.substring(1);
}
return name;
}
其实最后还是要把"/"去掉的...
来源:http://wangxuliangboy.javaeye.com/blog/345367
下面是我测试的结果:
上面为目录结构,下面的方法是在类org.fileupload.servlet.SecondUploadServlet中测试的
- System.out.println("this.getClass().getResource(*)--------------------------------");
- System.out.println(this.getClass().getResource("")); // .../classes/org/fileupload/servlet/
- System.out.println(this.getClass().getResource("/")); // .../classes/
- // 编译成class文件, 所以用.java是错的, 应用java
- System.out.println(this.getClass().getResource("./FirstUploadServlet.java")); // null
- System.out.println(this.getClass().getResource("/FirstUploadServlet.java")); // null
- System.out.println(this.getClass().getResource("FirstUploadServlet.java")); // null
- System.out.println(this.getClass().getResource("SecondUploadServlet.java")); // null
- System.out.println(this.getClass().getResource("org.fileupload.servlet")); // null
- System.out.println(this.getClass().getResource("org.fileupload.servlet.SecondUploadServlet.java")); // null
- // ./或最前面没有.和/表示当前目录, /表示类路径根目录, 即classes目录
- System.out.println(this.getClass().getResource("./FirstUploadServlet.class")); // .../classes/org/fileupload/servlet/./FirstUploadServlet.class
- System.out.println(this.getClass().getResource("/FirstUploadServlet.class")); // null
- System.out.println(this.getClass().getResource("FirstUploadServlet.class")); // .../classes/org/fileupload/servlet/FirstUploadServlet.class
- System.out.println(this.getClass().getResource("SecondUploadServlet.class")); // .../classes/org/fileupload/servlet/SecondUploadServlet.class
- System.out.println(this.getClass().getResource("/org/fileupload/servlet")); // .../classes/org/fileupload/servlet/
- System.out.println(this.getClass().getResource("org.fileupload.servlet.SecondUploadServlet.class")); // null
- System.out.println(this.getClass().getResource("test")); // .../classes/org/fileupload/servlet/test/
- System.out.println(this.getClass().getResource("./test")); // .../classes/org/fileupload/servlet/./test/
- System.out.println(this.getClass().getResource("test/OK.java")); // null
- System.out.println(this.getClass().getResource("/test/OK.java")); // null
- System.out.println(this.getClass().getResource("./test/OK.java")); // null
- System.out.println(this.getClass().getResource("test/OK.class")); // .../classes/org/fileupload/servlet/test/OK.class
- System.out.println(this.getClass().getResource("/test/OK.class")); // null
- System.out.println(this.getClass().getResource("./test/OK.class")); // .../classes/org/fileupload/servlet/./test/OK.class
- System.out.println(this.getClass().getResource("/classpath.jsp")); // .../classes/classpath.jsp
- System.out.println(this.getClass().getResource("./classpath.jsp")); // null
- System.out.println(this.getClass().getResource("classpath.jsp")); // null
- System.out.println("/n利用类加载器:");
- System.out.println("this.getClass().getClassLoader().getResource(*)-------------");
- System.out.println(this.getClass().getClassLoader().getResource("classpath.jsp"));
- System.out.println(this.getClass().getClassLoader().getResource("FirstUploadServlet.class")); // null
- System.out.println(this.getClass().getClassLoader().getResource("org/fileupload/servlet/FirstUploadServlet.class"));
- System.out.println(this.getClass().getClassLoader().getResource("/org/fileupload/servlet/FirstUploadServlet.class"));
- System.out.println(this.getClass().getClassLoader().getResource("./org/fileupload/servlet/FirstUploadServlet.class"));
- System.out.println("/nrequest.getRealPath(*)--------------------------------------");
- // 最前面有没有加左斜杆结果基本是一样的, 只是获取根目录时多或少了一右斜杆而已
- // 如果最前面为./, 那么结果会变成/./这样的路径
- System.out.println(request.getRealPath("")); // .../commons-fileupload
- System.out.println(request.getRealPath("/")); // .../commons-fileupload/
- System.out.println(request.getRealPath("index.jsp")); // .../commons-fileupload/index.jsp
- System.out.println(request.getRealPath("./index.jsp")); // .../commons-fileupload/./index.jsp
- System.out.println(request.getRealPath("/index.jsp")); // .../commons-fileupload/index.jsp
- System.out.println(request.getRealPath("fileupload/firstupload.jsp")); // .../commons-fileupload/fileupload/firstupload.jsp
- System.out.println(request.getRealPath("/fileupload/firstupload.jsp")); // .../commons-fileupload/fileupload/firstupload.jsp
- System.out.println(request.getRealPath("./fileupload/firstupload.jsp")); // .../commons-fileupload/./fileupload/firstupload.jsp
- System.out.println(request.getRealPath("./fileupload")); // .../commons-fileupload/./fileupload
- System.out.println(request.getRealPath("/fileupload")); // .../commons-fileupload/fileupload
- System.out.println(request.getRealPath("fileupload")); // .../commons-fileupload/fileupload
- // request.getSession().getServletContext().getRealPath与request.getRealPath结果一样
- System.out.println("/nrequest.getSession().getServletContext().getRealPath(*)------");
- // System.out.println(request.getSession().getServletContext().getRealPath("/")); // .../commons-fileupload/
- System.out.println(request.getSession().getServletContext().getRealPath("")); // .../commons-fileupload
- System.out.println(request.getSession().getServletContext().getRealPath("/")); // .../commons-fileupload/
- System.out.println(request.getSession().getServletContext().getRealPath("index.jsp")); // .../commons-fileupload/index.jsp
- System.out.println(request.getSession().getServletContext().getRealPath("./index.jsp")); // .../commons-fileupload/./index.jsp
- System.out.println(request.getSession().getServletContext().getRealPath("/index.jsp")); // .../commons-fileupload/index.jsp
- System.out.println(request.getSession().getServletContext().getRealPath("fileupload/firstupload.jsp")); // .../commons-fileupload/fileupload/firstupload.jsp
- System.out.println(request.getSession().getServletContext().getRealPath("/fileupload/firstupload.jsp")); // .../commons-fileupload/fileupload/firstupload.jsp
- System.out.println(request.getSession().getServletContext().getRealPath("./fileupload/firstupload.jsp")); // .../commons-fileupload/./fileupload/firstupload.jsp
- System.out.println(request.getSession().getServletContext().getRealPath("./fileupload")); // .../commons-fileupload/./fileupload
- System.out.println(request.getSession().getServletContext().getRealPath("/fileupload")); // .../commons-fileupload/fileupload
- System.out.println(request.getSession().getServletContext().getRealPath("fileupload")); // .../commons-fileupload/fileupload
- System.out.println("/nothers-------------------------------------------------------");
- System.out.println(request.getServletPath()); // /servlet/SecondUploadServlet
- System.out.println(request.getContextPath()); // /commons-fileupload
- System.out.println(request.getRequestURI()); // /commons-fileupload/servlet/SecondUploadServlet
- System.out.println(request.getRequestURL()); // http://localhost:8080/commons-fileupload/servlet/SecondUploadServlet