Showing posts with label servlets. Show all posts
Showing posts with label servlets. Show all posts

Tuesday, August 04, 2009

How do I convert Set into List?

Conversion between the collections made pretty easy in Java.

Converting from a Set/List to List/Set can be done by passing the Set/List instance to the constructor of List/Set such as ArrayList/HashSet respectively.

Following line of code performs the conversion (from Set to List):

Set strSet = new HashSet();
strSet.add("one")
.
.
.
strSet.add("xxx");

List strList = new ArrayList(set);

Above List can be converted back to Set using the following line of code:

Set setObject = new HashSetst(strList);

Following is the example program that does the entire same thing we discussed above:
-----
package com.blog.javaexposure.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Set2List {

public static void main(String rags[]) {
// Creating a Set object
Set set = new HashSet();
// Adding elements to the Set object
set.add("One");
set.add("Two");
set.add("Three");
set.add("Four");

// Checking the elements in the Set
System.out.println("Elements in the set are:");
for (Object obj : set) {
System.out.println(obj);
}

// Creating a list object
List list = new ArrayList(set);

// Checking the elements in the list
System.out.println("Elements in the list are:");
for (Object obj : list) {
System.out.println(obj);
}
// Removing elements from the set
set.clear();
System.out.println("Number of elements in the set are: "+set.size());

// Converting list to set
set = new HashSet(list);

// Checking the elements in the Set
System.out.println("Elements in the set are:");
for (Object obj : set) {
System.out.println(obj);
}
}
}
-----

Monday, August 03, 2009

Accessing properties file for efficient read/write










In previous post, we did not use any third party libraries for accessing the properties file. Here, we will be using the apache commons configuration API. You have to download the jar file and add it to the classpath.

Reading from a properties file:

Use the following line of code if you just want to read the properties file.

Configuration config = new PropertiesConfiguration(propertiesFileName);

Then you need to use the various methods available to read the propeties. It has some flexible methods such as to get the string type of values, or integer or a list of values.

Detailed information about Configuration Interface can be found here: org.apache.commons.Configuration

Writing to a properties file:

You have to use the PropertiesConfiguration object to write to the properties file.

Detailed information about PropertiesConfiguration can be found here: org.apache.commons.configuration.PropertiesConfiguration

Use the following code:

PropertiesConfiguration config;
try{
config = new PropertiesConfiguration(propertiesFileName);
config.setProperty(propertyName, propertyValue); //to update properties
config.save(); //writes the changes to the file
}
Catch(ConfigurationException e){
System.out.println("Configuration exception");
}

When you write/update the properties this way,
1. properties order in the file doesn't change
2. comments stays the same

Other Uses: We can save a back up of the properties file using the following method.

config.save(backupFileName). //backupFileName is the name of the back up file to be created

Some useful info from apache website can be found here

Sunday, August 02, 2009

Accessing properties file in a java apps

Properties file is a simple text file. It contains a set of key-value pairs. Each key is associated with a key. We use properties file to store the configuration properties so that we dont need to touch the code again n again and recompile n run the entire app. just editing the properties file should be enough.

I used the following code to load properties from a properties file in J2EE application. Following code shows how to read the properties file and load the available properties into Java.Util.Properties Object.

/* Reading a properties file */

Properties props = new Properties();

//loading the properties from file into "props"
try{
props.load(new FileInputStream(fileName);
//filename should be a string. for ex, abc.properties
}
Catch(IOException e){
System.out.println("IO Exception occurres while accessing the properties file"+fileName);
}

"props" contains all the properties available in the file. i.e. key/value pairs. It simply ignores the comments, if any, available in the file.

Following code shows how to write the properties into a file.
try{
props.store(new FileOutputStream(fileName), null);
}
Catch(IOException e){
System.out.println("IO Exception while accessing the properties file"+fileName);
}

PROS: no use of third party libraries and is easy to read and write from the properties file.
CONS: With this way of reading and writing to the files, all your comments will show at the starting of the file and then shows up the properties. So the comments (if any specific to the property will be gone and all comments mess up at the top of the file)
When to use: I recommend this way to use when your properties file doesn't have any comments. i.e. your properties file just contains key value pairs.

To know more about Java.Util.Properties: Java.Util.Properties

In my next post, I will discuss the efficient way to edit the properties file without affecting the comments and order of the properties.
È