Retrieving keys from config file

This is my config file

[a]

variableA=AAAA

variableB=BBBB

[x]

VariableC=CC

[c]

VariableD=D

VariableE=E

VariableF=F

How do I go about retrieving the keys, from section C? So in reality I am onlypulling into an array VariableD, VariableE, VariableF.

If someone could provide some sample code that would be best.

Thanks for your help.

Message was edited by:

ksuchoc1

[454 byte] By [ksuchoc1a] at [2007-11-27 10:24:41]
# 1

I would redo the format of my config file to be in XML or to be consistent

with the original property file format.

BigDaddyLoveHandlesa at 2007-7-28 17:29:22 > top of Java-index,Java Essentials,Java Programming...
# 2

That really isnt an option, since everything in the site operates around this config file, it is also tied in with other sites, that would be too much work to fix all sites. Thats why I am trying to create a workaround.

ksuchoc1a at 2007-7-28 17:29:22 > top of Java-index,Java Essentials,Java Programming...
# 3

So you already have code that parses that file?

BigDaddyLoveHandlesa at 2007-7-28 17:29:22 > top of Java-index,Java Essentials,Java Programming...
# 4

- The configuration files in java are usually properties file, which are simply a list of key,value pairs. Property files does not support multiple sections.

<jdk_install_dir>/jre/lib has several property files that are used by java itself.

Please take a look at http://java.sun.com/docs/books/tutorial/essential/environment/properties.html which has code snippets on reading property files.

As long as the key values are unique, you should use the recommended properties file mechanism. (You can still use comments to break the keys into different sections for human readability but a given key should only occur once in the file; otherwise only the last one will be read and the others will be ignored).

- The above will break if there can be multiple keys with same value. For instance, if your config file has windows ini file type construct, then property files cannot be used. (In your own example, the keys are unique; so property files should work. But if section [x] can also have a variableA defined along with section [a], property files wont work).

In such a case, you should consider defining your keys as section_keyname and then determine the section in code. Or you could have multiple property files for different sections. Or you may want to check out open-source programs like http://ini4j.sourceforge.net/index.html

KarthikRa at 2007-7-28 17:29:22 > top of Java-index,Java Essentials,Java Programming...
# 5

Not really, currently I hard code the below line for each item in the config file.

String VariableE = Config.getValue("VariableE", File);

And since the config files are getting rather lengthy I would like to just read in all the variables into an array and then use a loop to create the line above.

ksuchoc1a at 2007-7-28 17:29:22 > top of Java-index,Java Essentials,Java Programming...
# 6

Who wrote the Config class? Didn't they allow for reading all the variables at once?

You say the config file is getting lengthy. How lengthy -- over 1 lakh lines?

BigDaddyLoveHandlesa at 2007-7-28 17:29:22 > top of Java-index,Java Essentials,Java Programming...