宇筱博客

  • 解决办法
  • 学无止境
  • 记录时光
  • 百宝箱
宇筱博客
点滴记忆,汇聚成海。
  1. 首页
  2. 学无止境
  3. 正文

Java读取resource文件/路径的几种方式

2020年1月7日 488点热度 0人点赞 0条评论

方式一:

String fileName = this.getClass().getClassLoader().getResource("文件名").getPath();//获取文件路径
String fileUtl = this.getClass().getResource("文件名").getFile();
//(在项目打成jar后的情况下getPath()与getFile()返回参数及用法的基本相同具体差异大研究)
//示例路径结果:/E:/idea_work/sofn-qry-web/target/classes/CityJson.js

方式二:

File directory = new File("");//参数为空
String courseFile = directory.getCanonicalPath()//标准的路径 ;
String author =directory.getAbsolutePath()//绝对路径;
//(在jdk1.1后就有了此方法获取文件路径的方式存在了)
//示例路径结果:E:\idea_work\sofn-qry-web

方式三(博主用的这个方式):

java.net.URL uri = this.getClass().getResource("/");
(获取到Class文件存放的路径)
示例路径结果:file:/E:/idea_work/sofn-qry-web/target/test-classes/
String property =System.getProperty("user.dir");

方式四:

String property =System.getProperty("user.dir");
(此方法可以得到该工程项目所有文件的相关路径及环境配置信息)

原文链接:https://blog.csdn.net/oschina_40188932/java/article/details/78833754

标签: 暂无
最后更新:2020年1月7日

小渔民

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2025 宇筱博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备15017825号-2