Java获取路径的方法分析详解(Application/Web)

1、利用System.getProperty()函数获取当前路径:

System.getProperty("user.dir");//user.dir用户当前的工作目录,输出:D:\开发工程\GitHub\5_java_example\uritest\application,(如果想要通过此方法获取其它系统信息,参考:http://www.cnblogs.com/EasonJim/p/6507672.html

注意:以上输出为eclipse的application的输出。

测试JAR(Application):

正常,输出如下:

测试WAR(Web):

正常,输出如下:

总结:

①在jar中得到的是这个应用程序根目录,而且最后不带\,没有精确到具体哪个类。

②在war中得到的是tomcat的根目录,没有精确到webapps这个文件,更没有说是哪个web项目了。而在eclipse中,得到的是eclipse所在的目录。

 

System.getProperty("java.class.path");//java.class.path类路径,输出:D:\开发工程\GitHub\5_java_example\uritest\application\bin,(如果想要通过此方法获取其它系统信息,参考:http://www.cnblogs.com/EasonJim/p/6507672.html

注意:以上输出为eclipse的application的输出。

测试JAR(Application):

正常,输出如下:

测试WAR(Web):

正常,输出如下:

总结:

①在eclipse中调试application时,得到的路径觉得是完美,精确到了bin。但是如果导出jar包,得到的路径确实如上所示,没有达到目的。

②在war中得到的是tomcat中bin的jar包,如上所示,都是没有达到目的。

 

2、使用File提供的函数获取当前路径:  

参考:http://www.cnblogs.com/franson-2016/p/5728280.html

     try{ 
            File directory = new File("");//设定为当前文件夹
            System.out.println(directory.getCanonicalPath());//获取标准的路径,输出:D:\开发工程\GitHub\5_java_example\uritest\application 
            System.out.println(directory.getAbsolutePath());//获取绝对路径,输出:D:\开发工程\GitHub\5_java_example\uritest\application 
        }catch(Exception e){}         
        /*
         * File.getCanonicalPath()和File.getAbsolutePath()大约只是对于new File(".")和new File("..")两种路径有所区别。
         * 对于getCanonicalPath()函数,“."就表示当前的文件夹,而”..“则表示当前文件夹的上一级文件夹
         * 对于getAbsolutePath()函数,则不管”.”、“..”,返回当前的路径加上你在new File()时设定的路径
         * 至于getPath()函数,得到的只是你在new File()时设定的路径  
         */
        //比如当前的路径为 D:\开发工程\GitHub\5_java_example\uritest\application : 
        try{ 
            File directory1 = new File("abc"); 
            System.out.println(directory1.getCanonicalPath());//输出:D:\开发工程\GitHub\5_java_example\uritest\application\abc
            System.out.println(directory1.getAbsolutePath());//输出:D:\开发工程\GitHub\5_java_example\uritest\application\abc 
            System.out.println(directory1.getPath());//输出:abc 
        }catch(Exception e){}
        
        try{ 
            File directory2 = new File("."); 
            System.out.println(directory2.getCanonicalPath());//输出:D:\开发工程\GitHub\5_java_example\uritest\application 
            System.out.println(directory2.getAbsolutePath());//输出:D:\开发工程\GitHub\5_java_example\uritest\application\. 
            System.out.println(directory2.getPath());//输出:. 
        }catch(Exception e){} 
        
        try{ 
            File directory3 = new File(".."); 
            System.out.println(directory3.getCanonicalPath());//输出:D:\开发工程\GitHub\5_java_example\uritest 
            System.out.println(directory3.getAbsolutePath());//输出:D:\开发工程\GitHub\5_java_example\uritest\application\.. 
            System.out.println(directory3.getPath());//输出:.. 
        }catch(Exception e){} 

注意:以上输出为eclipse的application的输出。

测试JAR(Application):

正常,输出如下:

测试WAR(Web):

正常,输出如下:

总结:

①对于jar包来说,正常输出所在目录的路径。(换句话说,就是这个程序在哪运行,目录就定位在哪,但是不是精确到哪个类中的那种。)

②针对war包部署后,得到的是tomcat所在的根目录。没有精确到最根本的webapps目录。

③File对象获取的路径只精确到工作目录,而非class目录,所以在对于一些配置文件放置在class中,File对象获取时会比较麻烦,而且测试eclipse获取的路径和打包出jar时获取的路径是不一样的。因此在写法上需要来回切换。

④打包jar时,最终获取的是jar文件所在的路径,而这个路径是绝对的,所以没办法获取具体文件。而war包就是web容器的目录,所以再加工一下,也可以看成是相对目录,这个较容易处理。

 

3、在类中取得路径:

参考:http://www.cnblogs.com/yejg1212/p/3270152.html

提示:如果不想要file:可以使用getPath()方法获取,但是这样前面还是会有/出现,最终只能是使用截取的方式去除。

 1      //类的绝对路径,指的是编译后字节码class文件放置的位置根目录
 2         System.out.println(ApplicationTest.class.getResource(""));//当前类(class)所在的包目录,输出:file:/D:/%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b/GitHub/5_java_example/uritest/application/bin/applicationtest/
 3         System.out.println(Class.class.getResource(""));//当前类(class)所在的包目录,得到的会是null,因为没有这个具体的类,输出:null
 4         System.out.println(ApplicationTest.class.getResource("/"));//class path根目录,输出:file:/D:/%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b/GitHub/5_java_example/uritest/application/bin/
 5         System.out.println(Class.class.getResource("/"));//class path根目录,与上面效果一致,输出:file:/D:/%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b/GitHub/5_java_example/uritest/application/bin/
 6         System.out.println(ApplicationTest.class.getResource("/Test.xml"));//输出:file:/D:/%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b/GitHub/5_java_example/uritest/application/bin/Test.xml
 7         System.out.println(ApplicationTest.class.getResource("/applicationtest/Test2.xml"));//输出:file:/D:/%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b/GitHub/5_java_example/uritest/application/bin/applicationtest/Test2.xml
 8         System.out.println(Class.class.getResource("/applicationtest/Test2.xml"));//效果和上面一致,输出:file:/D:/%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b/GitHub/5_java_example/uritest/application/bin/applicationtest/Test2.xml
 9         System.out.println(ApplicationTest.class.getResource("applicationtest/Test2.xml"));//没有开头的斜杠,无法确定起始的class path根目录,输出:null
10         System.out.println(Class.class.getResource("applicationtest/Test2.xml"));//效果和上面一致,输出:null
11         //这里取资源文件有个技巧,在src文件夹下的资源文件,编译后都会按照原有的路径全部拷贝到class文件夹放置的位置下,因此会出现如上的取法
12         //当然,可以通过判断获取的对象是否为null来确定文件是否存在
13         //以下这种写法会自动查找当前文件,但是只能查找当前类所在的包下的文件,如果有子包存在时,要带上子包的路径,同时开头不需要斜杠。
14         System.out.println(ApplicationTest.class.getResource("Test.xml"));//不在当前类所在的包下,输出:null
15         System.out.println(ApplicationTest.class.getResource("Test3.xml"));//不在当前类所在的包下,输出:null
16         System.out.println(ApplicationTest.class.getResource("Test2.xml"));//不在当前类所在的包下,输出:file:/D:/%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b/GitHub/5_java_example/uritest/application/bin/applicationtest/Test2.xml
17         System.out.println(Class.class.getResource("Test2.xml"));//返回null,说明还是没有这个具体的类,所以找不到在哪里开始查找文件,输出:null
18         //总结:综上测试,具体的类获取资源文件可以免去很多麻烦,建议使用具体的类进行获取;还有中文路径会经过转码。

注意:以上输出为eclipse的application的输出。

测试JAR(Application):

正常,输出如下:

可以发现,这个输出和eclipse中的输出不一样,其中获取class path根目录和类所在的包目录已经无法获取。而且值的路径变了。

测试WAR(Web):

正常,输出如下:

同样可以发现路径都是url编码后的,中文也是如此。

总结:

①使用此方法,在jar和war中都能正常找到class的路径。

②推荐使用具体的类取查找资源。Class在这里就已经无效。

③资源文件的查找方式在jar和war包保持一致。

 

对比:Class.getClassLoader().getResource()

 1      //类的绝对路径,指的是编译后字节码class文件放置的位置根目录
 2         System.out.println(ApplicationTest.class.getClassLoader().getResource(""));//class path根目录,输出:file:/D:/%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b/GitHub/5_java_example/uritest/application/bin/
 3         //System.out.println(Class.class.getClassLoader().getResource(""));//报错
 4         System.out.println(ApplicationTest.class.getClassLoader().getResource("/"));//输出:null
 5         //System.out.println(Class.class.getClassLoader().getResource("/"));//报错
 6         System.out.println(ApplicationTest.class.getClassLoader().getResource("/Test.xml"));//输出:null
 7         System.out.println(ApplicationTest.class.getClassLoader().getResource("/applicationtest/Test2.xml"));//输出:null
 8         //System.out.println(Class.class.getClassLoader().getResource("/applicationtest/Test2.xml"));//报错
 9         System.out.println(ApplicationTest.class.getClassLoader().getResource("applicationtest/Test2.xml"));//没有开头的斜杠,输出:file:/D:/%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b/GitHub/5_java_example/uritest/application/bin/applicationtest/Test2.xml
10         //System.out.println(Class.class.getClassLoader().getResource("applicationtest/Test2.xml"));//报错
11         //这里取资源文件有个技巧,在src文件夹下的资源文件,编译后都会按照原有的路径全部拷贝到class文件夹放置的位置下,因此会出现如上的取法
12         //当然,可以通过判断获取的对象是否为null来确定文件是否存在
13         //以下这种写法会自动查找当前文件,但是只能查找class根目录下的文件,所在包下的文件无法找到
14         System.out.println(ApplicationTest.class.getClassLoader().getResource("Test.xml"));//输出:file:/D:/%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b/GitHub/5_java_example/uritest/application/bin/Test.xml
15         System.out.println(ApplicationTest.class.getClassLoader().getResource("Test3.xml"));//输出:null
16         System.out.println(ApplicationTest.class.getClassLoader().getResource("Test2.xml"));//输出:null
17         //System.out.println(Class.class.getClassLoader().getResource("Test2.xml"));//报错
18         //总结:综上测试,具体的类获取资源文件可以免去很多麻烦,建议使用具体的类进行获取;还有中文路径会经过转码。

 注意:以上结果为eclipse的application的输出。

可以发现,Class.class的全部报错了,而且ApplicationTest.class.getResource("/") == ApplicationTest.class.getClassLoader().getResource(""),也就是相反

测试JAR(Application):

正常,输出如下:

结果和上面eclipse输出的基本一致。

测试WAR(Web):

正常,输出如下:

可以看出这个和jar的方式有很大区别,不管开头有没有斜杠都能正常输出,而且都能找到这个文件Test2.xml。

总结:

①class.getClassLoader()这个方式在jar和war上其实有比较大的区别,如果为了统一的结果,建议不要使用这个方法。

②Class.class.getClassLoader可以用这个方法代替Thread.currentThread().getContextClassLoader(),效果一致。

③也可以用这个方法代替,和上面的效果一致:ClassLoader.getSystemClassLoader()。

 

4、File函数与类取路径结合输出:

     try{ 
            File directory1 = new File(ApplicationTest.class.getResource("/Test.xml").getPath()); 
            System.out.println(directory1.getCanonicalPath());//输出:D:\%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b\GitHub\5_java_example\uritest\application\bin\Test.xml
            System.out.println(directory1.getAbsolutePath());//输出: D:\%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b\GitHub\5_java_example\uritest\application\bin\Test.xml
            System.out.println(directory1.getPath());//输出:D:\%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b\GitHub\5_java_example\uritest\application\bin\Test.xml
        }catch(Exception e){}

 

注意:以上输出为eclipse的application的输出。

可以看出完美解决了上面方法出现file开头的问题。

测试JAR(Application):

正常,输出如下:

可以发现,完全为空,所以这个方法在jar中是不靠谱。

测试WAR(Web):

正常,输出如下:

总结:

①唯一的好处是去除了file开头。

 

5、(Web)在JSP中获取路径:

<%=request.getSession().getServletContext().getRealPath("") %><br/><!-- //得到工程目录, 参数可具体到包名。输出:D:\开发工程\workspace_web\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\webtest;对于获取资源文件的方法还可以这样用:request.getSession().getServletContext().getResource("WEB-INF/web.xml") -->
<%=request.getRequestURL() %><br/><!-- //得到IE地址栏地址。输出:http://localhost:8080/webtest/index.jsp -->
<%=request.getRequestURI() %><br/><!-- //得到相对地址,输出:/webtest/index.jsp -->
<%=request.getServletPath() %><br/><!-- //根目录所对应的绝对路径,输出:/index.jsp -->        
<%=request.getContextPath() %><br/><!-- //得到工程名,输出://webtest --> 

可以发现,和servlet没什么区别。

<%=application.getRealPath("index.jsp") %><br/><!-- //得到页面所在服务器的全路径,输出://D:\开发工程\workspace_web\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\webtest\index.jsp --> 

对于application的用法还有很多,比如获取文件夹的组合路径(输入upload)、根目录(输入/)即可。

 

6、(Web)在Servlet中获取路径:

     System.out.println(request.getSession().getServletContext().getRealPath(""));//得到工程目录, 参数可具体到包名。输出:D:\开发工程\workspace_web\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\webtest;对于获取资源文件的方法还可以这样用:request.getSession().getServletContext().getResource("WEB-INF/web.xml");比如getRealPath("/")得到的目录后面自动带上\或者/;
        System.out.println(request.getRequestURL());//得到IE地址栏地址。输出:http://localhost:8080/webtest/UriTest
        System.out.println(request.getRequestURI());//得到相对地址,输出:/webtest/UriTest
        System.out.println(request.getServletPath());//根目录所对应的绝对路径,输出:/UriTest
        System.out.println(request.getContextPath());//得到工程名,输出://webtest 

测试WAR(Web):

 

 

测试工程:https://github.com/easonjim/5_java_example/tree/master/uritest

转载于:https://www.cnblogs.com/EasonJim/p/6503612.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值