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

方式一:

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");
此方法可以得到该工程项目所有文件的相关路径及环境配置信息

示例输出结果:


  • 12
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值