网站宣传有文化事业建设费吗,无锡企业建站系统,网站开发的抓包,下载班级优化大师appJava路径 Java中使用的路径#xff0c;分为两种#xff1a;绝对路径和相对路径。具体而言#xff0c;又分为四种#xff1a; 一、URI形式的绝对资源路径 如#xff1a;file:/D:/java/eclipse32/workspace/jbpmtest3/bin/aaa.b URL是URI的特例。URL的前缀/协议#xff0c;… Java路径 Java中使用的路径分为两种绝对路径和相对路径。具体而言又分为四种 一、URI形式的绝对资源路径 如file:/D:/java/eclipse32/workspace/jbpmtest3/bin/aaa.b URL是URI的特例。URL的前缀/协议必须是Java认识的。URL可以打开资源而URI则不行。 URL和URI对象可以互相转换使用各自的toURI(),toURL()方法即可 二、本地系统的绝对路径 D:/java/eclipse32/workspace/jbpmtest3/bin/aaa.b Java.io包中的类需要使用这种形式的参数。 但是它们一般也提供了URI类型的参数而URI类型的参数接受的是URI样式的String。因此通过URI转换还是可以把URI样式的绝对路径用在java.io包中的类中。 三、相对于classpath的相对路径 如相对于 file:/D:/java/eclipse32/workspace/jbpmtest3/bin/这个路径的相对路径。其中bin是本项目的classpath。所有的Java源文件编译后的.class文件复制到这个目录中。 四、相对于当前用户目录的相对路径 就是相对于System.getProperty(user.dir)返回的路径。 对于一般项目这是项目的根路径。对于JavaEE服务器这可能是服务器的某个路径。这个并没有统一的规范 所以绝对不要使用“相对于当前用户目录的相对路径”。然而 默认情况下java.io 包中的类总是根据当前用户目录来分析相对路径名。此目录由系统属性 user.dir 指定通常是 Java 虚拟机的调用目录。 这就是说在使用java.io包中的类时最好不要使用相对路径。否则虽然在J2SE应用程序中可能还算正常但是到了J2EE程序中一定会出问题而且这个路径在不同的服务器中都是不同的 相对路径最佳实践 推荐使用相对于当前classpath的相对路径 因此我们在使用相对路径时应当使用相对于当前classpath的相对路径。 ClassLoader类的getResource(String name),getResourceAsStream(String name)等方法使用相对于当前项目的classpath的相对路径来查找资源。 读取属性文件常用到的ResourceBundle类的getBundle(String path)也是如此。 通过查看ClassLoader类及其相关类的源代码我发现它实际上还是使用了URI形式的绝对路径。通过得到当前classpath的URI形式的绝对路径构建了相对路径的URI形式的绝对路径。这个实际上是猜想因为JDK内部调用了SUN的源代码而这些代码不属于JDK不是开源的。 相对路径本质上还是绝对路径 因此归根结底Java本质上只能使用绝对路径来寻找资源。所有的相对路径寻找资源的方法都不过是一些便利方法。不过是API在底层帮助我们构建了绝对路径从而找到资源的 得到classpath和当前类的绝对路径的一些方法 下面是一些得到classpath和当前类的绝对路径的一些方法。你可能需要使用其中的一些方法来得到你需要的资源的绝对路径。 1.FileTest.class.getResource() 得到的是当前类FileTest.class文件的URI目录。不包括自己 如file:/D:/java/eclipse32/workspace/jbpmtest3/bin/com/test/ 2.FileTest.class.getResource(/) 得到的是当前的classpath的绝对URI路径。 如file:/D:/java/eclipse32/workspace/jbpmtest3/bin/ 3.Thread.currentThread().getContextClassLoader().getResource() 得到的也是当前ClassPath的绝对URI路径。 如file:/D:/java/eclipse32/workspace/jbpmtest3/bin/ 4.FileTest.class.getClassLoader().getResource() 得到的也是当前ClassPath的绝对URI路径。 如file:/D:/java/eclipse32/workspace/jbpmtest3/bin/ 5.ClassLoader.getSystemResource() 得到的也是当前ClassPath的绝对URI路径。 如file:/D:/java/eclipse32/workspace/jbpmtest3/bin/ 我推荐使用Thread.currentThread().getContextClassLoader().getResource()来得到当前的classpath的绝对路径的URI表示法。 Web应用程序中资源的寻址 上文中说过当前用户目录即相对于System.getProperty(user.dir)返回的路径。 对于JavaEE服务器这可能是服务器的某个路径这个并没有统一的规范 而不是我们发布的Web应用程序的根目录 这样在Web应用程序中我们绝对不能使用相对于当前用户目录的相对路径。 在Web应用程序中我们一般通过ServletContext.getRealPath(/)方法得到Web应用程序的根目录的绝对路径。 这样我们只需要提供相对于Web应用程序根目录的路径就可以构建出定位资源的绝对路径。 这是我们开发Web应用程序时一般所采取的策略。 *用来加载类下的资源文件属性文件等。 *getExtendResource(StringrelativePath)方法可以使用../符号来加载classpath外部的资源。 */ publicclass ClassLoaderUtil { privatestatic Log logLogFactory.getLog(ClassLoaderUtil.class); /** *Thread.currentThread().getContextClassLoader().getResource() */ /** *加载Java类。 使用全限定类名 *paramclassName *return */ publicstatic Class loadClass(String className) { try { return getClassLoader().loadClass(className); } catch (ClassNotFoundException e) { thrownew RuntimeException(class not found className, e); } } /** *得到类加载器 *return */ publicstatic ClassLoader getClassLoader() { return ClassLoaderUtil.class.getClassLoader(); } /** *提供相对于classpath的资源路径返回文件的输入流 *paramrelativePath必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源需要使用 ../来查找 *return 文件输入流 *throwsIOException *throwsMalformedURLException */ publicstatic InputStream getStream(String relativePath) throws MalformedURLException, IOException { if(!relativePath.contains(../)){ return getClassLoader().getResourceAsStream(relativePath); }else{ return ClassLoaderUtil.getStreamByExtendResource(relativePath); } } /** * *paramurl *return *throwsIOException */ publicstatic InputStream getStream(URL url) throws IOException{ if(url!null){ return url.openStream(); }else{ returnnull; } } /** * *paramrelativePath必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源需要使用 ../来查找 *return *throwsMalformedURLException *throwsIOException */ publicstatic InputStream getStreamByExtendResource(String relativePath) throws MalformedURLException, IOException{ return ClassLoaderUtil.getStream(ClassLoaderUtil.getExtendResource(relativePath)); } /** *提供相对于classpath的资源路径返回属性对象它是一个散列表 *paramresource *return */ publicstatic Properties getProperties(String resource) { Properties properties new Properties(); try { properties.load(getStream(resource)); } catch (IOException e) { thrownew RuntimeException(couldnt load properties file resource, e); } return properties; } /** *得到本Class所在的ClassLoader的Classpat的绝对路径。 *URL形式的 *return */ publicstatic String getAbsolutePathOfClassLoaderClassPath(){ ClassLoaderUtil.log.info(ClassLoaderUtil.getClassLoader().getResource().toString()); return ClassLoaderUtil.getClassLoader().getResource().toString(); } /** * *paramrelativePath 必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源需要使 用../来查找 *return资源的绝对URL *throwsMalformedURLException */ publicstatic URL getExtendResource(String relativePath) throws MalformedURLException{ ClassLoaderUtil.log.info(传入的相对路径relativePath) ; //ClassLoaderUtil.log.info(Integer.valueOf(relativePath.indexOf(../))) ; if(!relativePath.contains(../)){ return ClassLoaderUtil.getResource(relativePath); } String classPathAbsolutePathClassLoaderUtil.getAbsolutePathOfClassLoaderClassPath(); if(relativePath.substring(0, 1).equals(/)){ relativePathrelativePath.substring(1); } ClassLoaderUtil.log.info(Integer.valueOf(relativePath.lastIndexOf(../))) ; String wildcardStringrelativePath.substring(0,relativePath.lastIndexOf(../)3); relativePathrelativePath.substring(relativePath.lastIndexOf(../)3); int containSumClassLoaderUtil.containSum(wildcardString, ../); classPathAbsolutePath ClassLoaderUtil.cutLastString(classPathAbsolutePath, /, containSum); String resourceAbsolutePathclassPathAbsolutePathrelativePath; ClassLoaderUtil.log.info(绝对路径resourceAbsolutePath) ; URL resourceAbsoluteURLnew URL(resourceAbsolutePath); return resourceAbsoluteURL; } /** * *paramsource *paramdest *return */ privatestaticint containSum(String source,String dest){ int containSum0; int destLengthdest.length(); while(source.contains(dest)){ containSumcontainSum1; sourcesource.substring(destLength); } return containSum; } /** * *paramsource *paramdest *paramnum *return */ privatestatic String cutLastString(String source,String dest,int num){ // String cutSourcenull; for(int i0;i sourcesource.substring(0, source.lastIndexOf(dest, source.length()-2)1); } return source; } /** * *paramresource *return */ publicstatic URL getResource(String resource){ ClassLoaderUtil.log.info(传入的相对于classpath的路径resource) ; return ClassLoaderUtil.getClassLoader().getResource(resource); } /** *paramargs *throwsMalformedURLException */ publicstaticvoid main(String[] args) throws MalformedURLException { //ClassLoaderUtil.getExtendResource(../spring/dao.xml); //ClassLoaderUtil.getExtendResource(../../../src/log4j.properties); ClassLoaderUtil.getExtendResource(log4j.properties); System.out.println(ClassLoaderUtil.getClassLoader().getResource(log4j.properties).toString()); } } 后记 ClassLoaderUtil类的public static URL getExtendResource(String relativePath)虽然很简单但是确实可以解决大问题。 不过这个方法还是比较简陋的。我还想在未来有空时进一步增强它的能力。比如增加Ant风格的匹配符。用**代表多个目录*代表多个字符代表一个字符。达到Spring那样的能力一次返回多个资源的URL进一步方便大家开发。 总结 1.尽量不要使用相对于System.getProperty(user.dir)当前用户目录的相对路径。这是一颗×××随时可能要你的命。 2.尽量使用URI形式的绝对路径资源。它可以很容易的转变为URI,URLFile对象。 3.尽量使用相对classpath的相对路径。不要使用绝对路径。使用上面ClassLoaderUtil类的public static URL getExtendResource(String relativePath)方法已经能够使用相对于classpath的相对路径定位所有位置的资源。 4.绝对不要使用硬编码的绝对路径。因为我们完全可以使用ClassLoader类的getResource()方法得到当前classpath的绝对路径。 使用硬编码的绝对路径是完全没有必要的它一定会让你死的很难看程序将无法移植 如果你一定要指定一个绝对路径那么使用配置文件也比硬编码要好得多 当然我还是推荐你使用程序得到classpath的绝对路径来拼资源的绝对路径 转载于:https://blog.51cto.com/zhouhua/156306