Linux 拨号vps windows公众号手机端

java怎么获取项目resource路径

lewis 7年前 (2018-06-08) 阅读数 8 #程序编程
文章标签 Java

在Java中,可以通过以下方式获取项目的资源路径:

  1. 使用getClass().getResource()方法:
URL resourceUrl = getClass().getResource("/path/to/resource/file");
String resourcePath = resourceUrl.getPath();
  1. 使用ClassLoader.getResource()方法:
URL resourceUrl = getClass().getClassLoader().getResource("path/to/resource/file");
String resourcePath = resourceUrl.getPath();
  1. 使用Thread.currentThread().getContextClassLoader().getResource()方法:
URL resourceUrl = Thread.currentThread().getContextClassLoader().getResource("path/to/resource/file");
String resourcePath = resourceUrl.getPath();

以上方法中,"/path/to/resource/file"表示资源文件相对于src/main/resources目录的路径。请根据自己的文件结构进行相应的调整。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门