java - 从 InputStream(ByteArrayInputStream) 对象中获取绝对文件路径
问题描述
我有以下代码:
class Train{
static{
InputStream inpStr = Train.class.getClassLoader().getResourceAsStream("ABC.properties");
Properties props = new Properties();
props.load(inpStr);
}
}
我想知道这个文件 ABC.properties 的绝对文件路径,即 inpStr 从哪里读取它?通过调试,我意识到分配给 inpStr 的对象实际上是java.io.ByteArrayInputStream
. 但是我找不到获取绝对文件路径的方法。请帮忙
解决方案
First you need to get resource, not resourceAsStream:
URL resource = Train.class.getClassLoader().getResource("ABC.properties");
Then you get the path
Path path = Paths.get(resource.toURI());
And finally you can display the absolutePath
System.out.println(path.toAbsolutePath().toString());
推荐阅读
- maven - 软件构建和部署的最佳实践
- javascript - 如何在 Postman 上通过 Token 测试 API?
- python-3.x - 是否可以将 ARIMA 模型用于多索引时间序列数据帧?(使用 Python)
- amazon-web-services - 为 API 和 webapp/网站运行单独的 EC2 实例是个好主意吗?
- python - matplotlib 绘图问题
- java - HttpClient 执行但挂起,是否调用了 API?
- asp.net-core - 移动应用程序和身份验证 - B2C 与 ASP.NET Core 身份
- python - 在python中几次迭代后错误地附加了列表
- r - 如果需要通过 Duo 应用程序进行 MFA(多因素身份验证),如何通过 R 连接到 Snowflake 数据库?
- r - 如何以紧凑的形式描述回归交互项?