views:

58

answers:

3

An instance of java.util.Properties is passed to me which has been created using:

[...]
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? I had a glance at the API and couldn't see any easy way to do it.

+5  A: 

The file name (or path name in this case) is not stored in the Properties instance. In fact, you haven't even passed the name to the instance.

Tom Hawtin - tackline
+4  A: 

You can't. It's not saved in the Properties object.

gustafc
+1  A: 

You cannot get this information, as a Properties object is not necessarily linked to a File...

Indeed, you can populate the Properties in several ways:

  • Load a properties file (as you did in your example).
  • Populate directly this object (using put() method from the Hashtable class).
romaintaz