相对路径获取文件

//获取文件File file = new File(url);

//这个获取的是未编译的本地文件
File file = new File("src/main/resources/templates/changeActionRichText/freemarker/ChangeActionRichText.html");
//由于打包后不存在什么src地址,所以不要代码中用上面的方式获取。用下面的方式获取编译后的相对地址
//前者用getResource获取到地址,后者用getClassLoader().getResourceAsStream
URL url1 = this.getClass().getResource("/");    //这里获取的是bin目录,也就是编译后的根目录
InputStream inputStreamRoot = this.getClass().getClassLoader().getResourceAsStream("/");//这里获取的是bin目录,也就是编译后的根目录
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(						"templates/changeActionRichText/freemarker/ChangeActionRichText.html");
URL url2 = this.getClass().getResource("/templates/changeActionRichText/freemarker/ChangeActionRichText.html");        //假设想获取这个文件的地址
//获取文件中的内容
String info = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining(System.lineSeparator()));
//获取文件中的内容
String info = FileUtils.readFileToString(file);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值