java读取文件的两种方法:java.io和java.lang.ClassLoader (我就知道这两种.....)java
// java.io: File file = new File("..."); FileInputStream fis = new FileInputStream("..."); FileReader fr = new FileReader("..."); //ClassLoader: ClassLoader loader = XXXClass.class.getClassLoader(); ClassLoader loader2 = Thread.currentThread().getContextClassLoader(); URL url = loader.getResource("..."); File file = new File(url.getFile()); InputStream input = loader.getResourceAsStream("...");
java.io 包中的类老是根据当前用户目录来分析相对路径名,也就是说相对路径是否好使,取决于 user.dir 的值。系统属性 user.dir 是 JVM 启动的时候设置的,一般是 Java 虚拟机的调用目录,即执行 java 命令所在的目录。web
对于 tomcat/jboss 容器,user.dir 是 %home/bin%/
目录,由于这个目录就是咱们启动 web 容器的地方spring
在 eclipse 中运行程序的时候,eclipse 会将 user.dir 的值设置为工程的根目录tomcat
用户目录可使用 System.getProperty("user.dir")
来查看框架
因此说,使用 java.io 读取文件,不管是相对路径,仍是绝对路径都不是好的作法,能不使用就不要使用(在 JavaEE 中)。eclipse
Class.getResource() 有 2 种方式,绝对路径和相对路径。绝对路径以 /
开头,从 classpath 或 jar 包根目录下开始搜索;this
相对路径是相对当前 class 所在的目录,容许使用 ..
或 .
来定位文件。url
ClassLoader.getResource() 只能使用绝对路径,并且不用以 /
开头。spa
这两种方式读取资源文件,不会依赖于 user.dir,也不会依赖于具体部署的环境,是推荐的作法(JavaEE).net
java.io:
相对于当前用户目录的相对路径读取;注重与磁盘文件打交道或者纯 java project 中使用。
虽然 ClassLoader 方式更通用,可是若是不是 javaEE 环境,要定位到 classpath 路径下去读文件是不合理的。
java.lang.ClassLoader:
相对于 classpath 的相对路径读取;建议在 javaEE 环境中都使用这种方式。
一般,ClassLoader 不能读取太大的文件,它适合读取 web 项目的那些配置文件,若是须要读取大文件,仍是要用 IO 包下的,能够先经过 ClassLoader 获取到文件的绝对路径,而后传给 File 或者其余对象,用 io 包里的对象去读取会更好些
在 JavaEE 中获取路径还有一直方式,那就是 ServletContext 的 getRealPath 方法,它能够得到物理路径。
参数中 '/'
就表示当前的工程所在的目录,能够理解为 WebRoot 所在的那个目录。
Spring 能够说是 JavaWeb 开发不可或缺的框架,它有提供 ClassPathResource 来读取文件:
/** * This implementation opens an InputStream for the given class path resource. * @see java.lang.ClassLoader#getResourceAsStream(String) * @see java.lang.Class#getResourceAsStream(String) */ public InputStream getInputStream() throws IOException { InputStream is; if (this.clazz != null) { is = this.clazz.getResourceAsStream(this.path); } else { is = this.classLoader.getResourceAsStream(this.path); } if (is == null) { throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist"); } return is; }
能够看出 spring 提供的 ClassPathResource,底层使用的就是 Class.getResource 或 ClassLoader.getResource()
http://blog.csdn.net/aitangyong/article/details/36471881