Friday, 1 May 2015

Properties class in Java

Properties class in Java

The properties object contains key and value pair both as a string. It is the subclass of Hashtable.
It can be used to get property value based on the property key. The Properties class provides methods to get data from properties file and store data into properties file. Moreover, it can be used to get properties of system.

Advantage of properties file

Easy Maintenance: If any information is changed from the properties file, you don't need to recompile the java class. It is mainly used to contain variable information i.e. to be changed.

Methods of Properties class

The commonly used methods of Properties class are given below.
Method
Description
public void load(Reader r)
loads data from the Reader object.
public void load(InputStream is)
loads data from the InputStream object
public String getProperty(String key)
returns value based on the key.
public void setProperty(String key,String value)
sets the property in the properties object.
public void store(Writer w, String comment)
writers the properties in the writer object.
public void store(OutputStream os, String comment)
writes the properties in the OutputStream object.
storeToXML(OutputStream os, String comment)
writers the properties in the writer object for generating xml document.
public void storeToXML(Writer w, String comment, String encoding)
writers the properties in the writer object for generating xml document with specified encoding.

Example of Properties class to get information from properties file

To get information from the properties file, create the properties file first.
db.properties
1.    user=system  
2.    password=oracle  
Now, lets create the java class to read the data from the properties file.
Test.java
1.    import java.util.*;  
2.    import java.io.*;  
3.    public class Test {  
4.    public static void main(String[] args)throws Exception{  
5.        FileReader reader=new FileReader("db.properties");  
6.          
7.        Properties p=new Properties();  
8.        p.load(reader);  
9.          
10.     System.out.println(p.getProperty("user"));  
11.     System.out.println(p.getProperty("password"));  
12. }  
13. }  
Output:system
       oracle
Now if you change the value of the properties file, you don't need to compile the java class again. That means no maintenance problem.

Example of Properties class to get all the system properties

By System.getProperties() method we can get all the properties of system. Let's create the class that gets information from the system properties.
Test.java
1.    import java.util.*;  
2.    import java.io.*;  
3.    public class Test {  
4.    public static void main(String[] args)throws Exception{  
5.      
6.    Properties p=System.getProperties();  
7.    Set set=p.entrySet();  
8.      
9.    Iterator itr=set.iterator();  
10. while(itr.hasNext()){  
11. Map.Entry entry=(Map.Entry)itr.next();  
12. System.out.println(entry.getKey()+" = "+entry.getValue());  
13. }  
14.   
15. }  
16. }  
Output:
java.runtime.name = Java(TM) SE Runtime Environment
sun.boot.library.path = C:\Program Files\Java\jdk1.7.0_01\jre\bin
java.vm.version = 21.1-b02
java.vm.vendor = Oracle Corporation
java.vendor.url = http://java.oracle.com/
path.separator = ;
java.vm.name = Java HotSpot(TM) Client VM
file.encoding.pkg = sun.io
user.country = US
user.script = 
sun.java.launcher = SUN_STANDARD
...........

Example of Properties class to create properties file

Now lets write the code to create the properties file.
Test.java
1.    import java.util.*;  
2.    import java.io.*;  
3.    public class Test {  
4.    public static void main(String[] args)throws Exception{  
5.      
6.    Properties p=new Properties();  
7.    p.setProperty("name","Bhupender Dagar");  
8.    p.setProperty("email","bsdagar01@gmail.com");  
9.      
10. p.store(new FileWriter("info.properties"),"My java Examples");  
11.   
12. }  
13. }  
Let's see the generated properties file.
info.properties
1.    #My java Examples
2.    #Thu Oct 03 22:35:53 IST 2013  
3.    email=bsdagar01@gmail.com  
4.    name=Bhupender Dagar 


Difference between ArrayList and Vector
ArrayList and Vector both implements List interface and maintains insertion order.
But there are many differences between ArrayList and Vector classes that are given below.
ArrayList
Vector
1) ArrayList is not synchronized.
Vector is synchronized.
2) ArrayList increments 50%of current array size if number of element exceeds from its capacity.
Vector increments 100% means doubles the array size if total number of element exceeds than its capacity.
3) ArrayList is not a legacyclass, it is introduced in JDK 1.2.
Vector is a legacy class.
4) ArrayList is fast because it is non-synchronized.
Vector is slow because it is synchronized i.e. in multithreading environment, it will hold the other threads in runnable or non-runnable state until current thread releases the lock of object.
5) ArrayList uses Iteratorinterface to traverse the elements.
Vector uses Enumeration interface to traverse the elements. But it can use Iterator also.
Example of Java ArrayList
Let's see a simple example where we are using ArrayList to store and traverse the elements.
1.    import java.util.*;    
2.    class TestArrayList21{    
3.     public static void main(String args[]){    
4.         
5.      List<String> al=new ArrayList<String>();//creating arraylist    
6.      al.add("Sonoo");//adding object in arraylist    
7.      al.add("Michael");    
8.      al.add("James");    
9.      al.add("Andy");    
10.   //traversing elements using Iterator  
11.   Iterator itr=al.iterator();  
12.   while(itr.hasNext()){  
13.    System.out.println(itr.next());  
14.   }    
15.  }    
16. }    

Output:
Sonoo
Michael
James
Andy
Example of Java Vector
Let's see a simple example of java Vector class that uses Enumeration interface.
1.    import java.util.*;      
2.    class TestVector1{      
3.     public static void main(String args[]){      
4.      Vector<String> v=new Vector<String>();//creating vector  
5.      v.add("umesh");//method of Collection  
6.      v.addElement("irfan");//method of Vector  
7.      v.addElement("kumar");  
8.      //traversing elements using Enumeration  
9.      Enumeration e=v.elements();  
10.   while(e.hasMoreElements()){  
11.    System.out.println(e.nextElement());  
12.   }  
13.  }      

14. }      

Output:
umesh
irfan
kumar

No comments:

Post a Comment

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...