You are not logged in.

#1 2010-08-19 16:09:33

samuele.mattiuzzo
Member
From: Treviso, IT
Registered: 2009-10-12
Posts: 307
Website

Java properties files: direct access to an element

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 sad do you have any hint for me?
thanks!

Offline

#2 2010-08-19 16:11:14

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: Java properties files: direct access to an element

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


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#3 2010-08-19 16:13:08

samuele.mattiuzzo
Member
From: Treviso, IT
Registered: 2009-10-12
Posts: 307
Website

Re: Java properties files: direct access to an element

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

#4 2010-08-19 16:15:41

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: Java properties files: direct access to an element

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)


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#5 2010-08-19 16:21:37

samuele.mattiuzzo
Member
From: Treviso, IT
Registered: 2009-10-12
Posts: 307
Website

Re: Java properties files: direct access to an element

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 smile

Offline

Board footer

Powered by FluxBB