Analytics


Google

Wednesday, September 3, 2008

Java Configuration File vs .Net Configuration File

.Net

When using ASP.Net, the configuration file is known as web.config and is located in the root of the application folder (virtual folder designated application in IIS). For applications, the config file will follow the name of the application so if your Application is called StartProg.exe the config file will be StartProg.exe.config.

The config file is a xml file and the content will look something like this:
<configuration>
<appSettings>
<add key="orString" value="TestApp is logsmith;aaxx" />
</appSettings>
</configuration>
There are other sections in .Net configuration but I am only going to focus on the AppSettings.

To retrieve the information, you use: System.Configuration.ConfigurationSettings.AppSettings("orString").

In Framework 2.0, this has been change:
System.Configuration.ConfigurationManager.AppSettings("orString").

The value in the parameter is case sensitive. To retrieve the value, you do as follows:

Dim tmpStr As String

tmpStr =
System.Configuration.ConfigurationSettings.AppSettings("orString")

Java

Java does not have a default file naming convention, you can name it anyway you want. But to ease my finding, I will follow the .Net method which is to name the config file similar to the .jar file. So if the jar file is StartProg.jar, I will name my config file as StartProg.config.

In Java, your config file can be a xml or not. Here is a little program, I wrote to test the concept:

package com.testJavaApp;

import java.io.IOException;

/**
*
* @author strovek
*/
public class clsTest {

static String fName = "clsFile.config";
static String fName2 = "clsFile.config2";

public String getVer() {
String vVer = new String();

java.lang.Package p = this.getClass().getPackage();
vVer = p.getImplementationVersion();

return vVer;
}

public void writeProp() throws java.lang.Exception {
try {
java.util.Properties vProp = new java.util.Properties();
String tmpStr = new String();

tmpStr = "TestApp is logsmith;aaxx";

vProp.setProperty("orString", tmpStr);
java.io.FileOutputStream configFile = new java.io.FileOutputStream(fName);
vProp.storeToXML(configFile, "Configuration File");
configFile.close();
configFile = new java.io.FileOutputStream(fName2);
vProp.store(configFile, "Second File");
configFile.close();


} catch (java.lang.Exception ex) {
throw new java.lang.Exception("[clsTest01]" + ex.getMessage());
}
}

public void readProp() throws java.lang.Exception {
try {
java.util.Properties vProp = new java.util.Properties();
java.io.FileInputStream iFile = new java.io.FileInputStream(fName);

vProp.loadFromXML(iFile);
String tmpStr = new String();
tmpStr = vProp.getProperty("orString");
System.out.println("ConnString is \n" + tmpStr);

vProp = new java.util.Properties();
iFile = new java.io.FileInputStream(fName2);
vProp.load(iFile);
tmpStr = vProp.getProperty("orString");
System.out.println("ConnString is \n" + tmpStr);



} catch (java.lang.Exception ex) {
throw new java.lang.Exception("[readProp01]" + ex.getMessage());
}

}
}



The config file generated are as follows:

XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Configuration File</comment>
<entry key="orString">TestApp is logsmith;aaxx</entry>
</properties>


nonXML

#Second File
#Fri Aug 29 11:43:43 SGT 2008
orString=TestApp is logsmith;aaxx

As you can see from the sample program, java.util.Properties is the class to read and write the configuration file. A few things to note:
  • use .loadFromXML if the configuration file is in XML format.
  • use .load for nonXML.
  • use FileInputStream to read, then you can specify the config file name to load
  • use FileOutputStream to write the configuration file (the whole configuration).
  • use .storeToXML to create a XML configuration file.
  • use .store to create a nonXML configuration file.
I use the above sample file to provide me a template to create subsequent configuration file for my other projects.

3 comments:

Erik Vermeulen said...

Thanks! This is handy!

By the way: it's java.util.Properties instead of java.lang.Properties. But your source code made this clear already.

Strovek said...

Thanks Erik. I have corrected the typo in the post.

Anonymous said...

I hope you know that adding java.lang. before a Object call isn't necessary.