6 Replies Latest reply: Jul 12, 2007 11:39 AM by 807605 RSS

    Retrieving keys from config file

    807605
      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 only pulling 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
        • 1. Re: Retrieving keys from config file
          807605
          I would redo the format of my config file to be in XML or to be consistent
          with the original property file format.
          • 2. Re: Retrieving keys from config file
            807605
            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.
            • 3. Re: Retrieving keys from config file
              807605
              So you already have code that parses that file?
              • 4. Re: Retrieving keys from config file
                807605
                - 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
                • 5. Re: Retrieving keys from config file
                  807605
                  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.
                  • 6. Re: Retrieving keys from config file
                    807605
                    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?