Monday, July 6, 2009

Java Source Code to read from and write into a properties files

In many of applications we are developing in many of our live projects, we might find that there are different environments that we use while development phase, testing phase and production phase.

The paths, the input file and output file locations etc are varying in different environments and we generally build three different jars/wars/ears for three different environments.


However, if we use a file which contains the locations which can vary from environment to environment, we can easily build a single archive file and deploy into the environment with environment specific files.


These environment specific files can be achieved using Properties class of the java.util package and the files are called properties file.


Though I have given example of the environment, we can use heavily in each applications where there may be a susceptible change required frequently.


This will not only reduce the time to code each time, it will also reduce the cost for any changes required [if can be incorporated with properties files].

The code here does nothing but reading from properties file (SampleProperties.properties) and writing specific values into the properties file. Please see the entries and the outputs of the Properties file.

The properties file can be written in notepad and has to be saved with .properties extension.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class SampleProperties {
private Properties props;
private File fName = new File("./SampleProperties.properties");
public SampleProperties() {
props = new Properties();
try {
FileInputStream fis = new FileInputStream(fName);
props.load(fis);
} catch (FileNotFoundException fNFE){
fNFE.printStackTrace();
} catch (IOException iOEx){
iOEx.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
public String getPathProperties(String propName) {
String retProp=new String("");
retProp = props.getProperty(propName);
return retProp;
}

public void setPathProperties(String propName,String propValue) {
try {
props.setProperty(propName,propValue);
props.store(new FileOutputStream(fName),"Updated");
props.load(new FileInputStream(fName));
} catch (FileNotFoundException fNFE) {
fNFE.printStackTrace();
} catch (IOException iOEx) {
iOEx.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}

public static void main(String[] args) {
SampleProperties sampleProps = new SampleProperties();
System.out.println("Value of key1 "+sampleProps.getPathProperties("key1"));
sampleProps.setPathProperties("key2","value2");
sampleProps.setPathProperties("key4","value4");
}
}
//End of class



SampleProperties.properties

Inputs:

Entries in the properties:


key1=value1
key2=value_Diff
key3=value3

Output (result of the writing into the properties file):

key4=value4
key3=value3
key2=value2
key1=value1



Hope you all will find it useful.

No comments:

Total Pageviews