The chances are all you really need are some name-value pairs to specify a few parameters. If this is the case Java comes with a wonderfully simple solution right out of the box, .properties files.
The syntax for a .properties file could not be simpler; all lines beginning with a # are comments and all other lines are in the form:
CODE:
1.
< key>= < value>
Below is a sample configuration for the Virtual Learning environment
CODE:
1.
# Basic portal config
2.
BASE_URL=www.eve.nuim.ie/evePortal
3.
4.
# Database stuff
5.
DB_JNDI_NAME=jdbc/eve_portal
6.
7.
# File locations
8.
DATA_DIR=/var/eve-data
9.
PDFLATEX_BIN=/opt/local/bin/pdflatex
10.
IMAGEMAGICK_BIN_DIR=/opt/local/bin
If you choose to use this type of configuration file you can locate and open it all in one step by using functionality built into the java class loader as follows:
JAVA:
1.
Properties configFile = new Properties();
2.
configFile.load(this.getClass().getClassLoader().getResourceAsStream("/my_config.properties"));
You can they read out any key in the following way:
JAVA:
1. some_var = configFile.getProperty("some_key");
Simple as that, no need to load complex parsing libraries, no big long messy code. Unless you have a good reason to go with a more complex format you really should be using .properties files.
Properties configFile = new Properties();
configFile.load(new FileInputStream(”configuration.conf”));
String foo = configFile.getProperty(”my_key”);
No comments:
Post a Comment