【问题标题】:Get the absolute file path from InputStream(ByteArrayInputStream) object从 InputStream(ByteArrayInputStream) 对象中获取绝对文件路径
【发布时间】:2020-03-11 11:26:54
【问题描述】:

我有以下代码:

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】:

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

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

    然后你得到路径

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

    最后你可以显示absolutePath

    System.out.println(path.toAbsolutePath().toString());
    

    【讨论】:

      猜你喜欢
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 2012-04-24
      • 2018-07-12
      • 2013-11-17
      相关资源
      最近更新 更多