Java class.getClassLoader().getResource(““) 获取资源路径 您所在的位置:网站首页 idea的resources目录获取 Java class.getClassLoader().getResource(““) 获取资源路径

Java class.getClassLoader().getResource(““) 获取资源路径

2024-07-01 23:01| 来源: 网络整理| 查看: 265

Java class.getClassLoader().getResource("") 获取资源路径

在开发中经常需要获取资源文件路径,例如读写配置文件等。Java也提供很多方法来获取这些路径,下面就几种常用到的作一下讨论区分:

1、xxx.class.getClassLoader().getResource(“”).getPath(); 获取src资源文件编译后的路径(即classes路径)

2、xxx.class.getClassLoader().getResource(“文件”).getPath(); 获取classes路径下“文件”的路径

3、xxx.class.getResource(“”).getPath(); 缺少类加载器,获取xxx类经编译后的xxx.class路径

4、this.getClass().getClassLoader().getResource(“”).getPath(); 以上三种方法的另外一种写法

System.out.println(FileUpLoadAction.class.getClassLoader().getResource("conf.properties").getPath()); System.out.println(FileUpLoadAction.class.getClassLoader().getResource("").getPath()); System.out.println(FileUpLoadAction.class.getResource("").getPath()); System.out.println(this.getClass().getClassLoader().getResource("").getPath());

输出依次为:

/D:/codes/trunk/client/web/IBMS/myworkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webclient/WEB-INF/classes/conf.properties /D:/codes/trunk/client/web/IBMS/myworkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webclient/WEB-INF/classes/ /D:/codes/trunk/client/web/IBMS/myworkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webclient/WEB-INF/classes/com/gmi/client/ /D:/codes/trunk/client/web/IBMS/myworkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webclient/WEB-INF/classes/ D:\codes\trunk\client\web\IBMS\myworkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\webclient\login.jsp 1


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有