java怎么获取项目resource路径

avatar
作者
猴君
阅读量:1

在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目录的路径。请根据自己的文件结构进行相应的调整。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!