简体   繁体   English

从属性实例获取使用的文件名

[英]Getting the used file name from a Properties instance

An instance of java.util.Properties is passed to me which has been created using: 一个java.util.Properties实例被传递给我,它是使用以下方法创建的:

[...]
properties = new java.util.Properties();

try {
  properties.load(
    AutoProperties.class.getClassLoader().getResourceAsStream(propertyFile)
  );
}
[...]

I was wondering how I could retrieve the file name ( propertyFile above) from the properties instance? 我想知道如何从properties实例检索文件名(上面的propertyFile )? I had a glance at the API and couldn't see any easy way to do it. 我看了一眼API,却看不到任何简单的方法。

The file name (or path name in this case) is not stored in the Properties instance. 文件名(在这种情况下为路径名)未存储在Properties实例中。 In fact, you haven't even passed the name to the instance. 实际上,您甚至没有将名称传递给实例。

You can't. 你不能 It's not saved in the Properties object. 它没有保存在Properties对象中。

You cannot get this information, as a Properties object is not necessarily linked to a File... 您无法获取此信息,因为Properties对象不一定链接到File ...

Indeed, you can populate the Properties in several ways: 实际上,您可以通过以下几种方式填充Properties

  • Load a properties file (as you did in your example). 加载属性文件(如您在示例中所做的那样)。
  • Populate directly this object (using put() method from the Hashtable class). 直接填充此对象(使用Hashtable类中的put()方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM