从InputStream(ByteArrayInputStream)对象获取绝对文件路径

问题描述 投票:1回答:1

我有以下代码:

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。但是我无法找到获取取消文件路径的方法。请帮助

java classpath inputstream bytearrayinputstream
1个回答
0
投票

首先,您需要获取资源,而不是resourceAsStream:

URL resource = Train.class.getClassLoader().getResource("ABC.properties");

然后您得到路径

Path path = Paths.get(resource.toURI());

最后,您可以显示absolutePath

System.out.println(path.toAbsolutePath().toString());
© www.soinside.com 2019 - 2024. All rights reserved.