wood burning stoves*
The moose likes Beginning Java and the fly likes Getting the Key from a properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting the Key from a properties file" Watch "Getting the Key from a properties file" New topic
Author

Getting the Key from a properties file

Neil Tomlin
Greenhorn

Joined: Nov 15, 2010
Posts: 9
    
    1
Hi,

I'm currently struggling to find out if there is a way I can get the Key from the Key-Value pair in a properties file.

As far as I am aware getProperty(String Key) can return the value from the Key-Value pair, but what if I know the Value but want to find out what the Key is.

Is this possible?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65



Now what?

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Neil Tomlin
Greenhorn

Joined: Nov 15, 2010
Posts: 9
    
    1
well I'm thinking particularly of the mime.types file that I have loaded into a properties file

eg
where:
key value
image/gif gif


I know that my file type is a gif
I want image/gif

Can I get it?

Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
Properties extends Hashtable so...you have whatever you can do on a Hashtable available to you. Plus Properties has a method public Enumeration propertyNames()
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
Neil Tomlin wrote:well I'm thinking particularly of the mime.types file that I have loaded into a properties file

eg
where:
key value
image/gif gif


I know that my file type is a gif
I want image/gif

Can I get it?


Reverse your key/value pairs. Especially in this case it would seem to make sense...what if you have more than one file extension for which you want to use the same mime type? You can have duplicate values but you can't have duplicate keys. Imagine you have .html and .htm and you want to use the same mime type 'text/html' for both.


You can have this in your properties file:

htm=text/html
html=text/html

but you can't have this:
text/html=htm
text/html=html

One of these values will be overwritten by the other in the underlying hashtable. The other alternative could be do something like:

text/html=htm,html

and tokenize the property value on the comma
Neil Tomlin
Greenhorn

Joined: Nov 15, 2010
Posts: 9
    
    1
Apologies to continue asking questions, but how can I reverse it? I've just loaded it into a properties file straight from mime.types.
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
Neil Tomlin wrote:Apologies to continue asking questions, but how can I reverse it? I've just loaded it into a properties file straight from mime.types.


Since you said you loaded it into a Properties FILE I assumed you meant you literally copied the content of the mime.types file into a .properties file and that you were then reading in that .properties file so I assumed you could edit the .properties file since I assumed you had created it. Now, I'm guessing what you probably mean to say is that you loaded the mime.types file into a Properties object??
Neil Tomlin
Greenhorn

Joined: Nov 15, 2010
Posts: 9
    
    1
That is correct.

Sorry!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting the Key from a properties file
 
Similar Threads
How to read a .properties file through script
save properties file without affecting existing comments.
ActionMessage and showing message to jsp
error
Design Patter for the login process