vahidg
vahidg

Reputation: 3963

Getting the used file name from a Properties instance

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.

Upvotes: 2

Views: 2302

Answers (3)

Romain Linsolas
Romain Linsolas

Reputation: 81627

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).

Upvotes: 1

gustafc
gustafc

Reputation: 28865

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

Upvotes: 4

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

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.

Upvotes: 6

Related Questions