You are not logged in.
Hello,
i have to deal with java properties files. Actually i have a file called prop.properties and it's structured like this
index1=1
name1=asd
index2=2
name2=rotfl
val2=10
which suits my needs. To read values i have a loop from 0 to 10, which i use to read "<key_name>+<index>" from properties (example: at loop 0, index = 0, so i'm searching for "index0","name0" and so on).
As i understood, this means my script everytime loops through the whole file to find the correct key, and i was thinking if maybe there was a quick way to direct access every element, instead of reading all n-1 elements before the nth i need, but googling around still didn't help me do you have any hint for me?
thanks!
Offline
Looping through properties file is a bad idea. You can do something like this.
System.getProperty("propertyname");
Or if all your properties relate to a particular module that you have in your code, you can even create a custom Properties object
There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !
Offline
that's what i'm using now, but my teacher told me it looks for every other element before that one, instead of doing a direct access to that element... or is that wrong?
Offline
http://download.oracle.com/javase/1.4.2 … ng.String)
Hmm I am not entirely sure about the inner workings of the System.getProperty() call.
But if you want you can always create a Map of all the properties and then use that in place of a properties file.
EDIT : The more I think about it, I am sure the getProperty also uses a hashtable behind the scenes. It would be wasteful to use a list for properties file which is nothing but a key-value pair
Last edited by Inxsible (2010-08-19 16:18:23)
There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !
Offline
reading your reply gave me the correct input of search and i went looking for this:
java.util
Class Properties
java.lang.Object
extended byjava.util.Dictionary
extended byjava.util.Hashtable
extended byjava.util.Properties
All Implemented Interfaces:
Cloneable, Map, Serializable
so i guess it uses a map or an hashtable to do the search, and the getProperty() call does what i need
Offline