Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting the Key from a properties file

 
Neil Tomlin
Greenhorn
Posts: 9
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now what?
 
Neil Tomlin
Greenhorn
Posts: 9
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct.

Sorry!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic