Tuesday 6 March 2012

Create Empty DOM Document


package com.usr.xml.dom;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class CreateEmptyDOMDocument {
public static void main(String[] args) {
       System.out.println("creating empty DOM Document");
       try{
              DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
              DocumentBuilder db=dbf.newDocumentBuilder();
              Document doc=db.newDocument();
       }
       catch(Exception ex){
              ex.printStackTrace();
       }
       System.out.println("empty DOM Document created");
}
}

java program to read properties xml file

userinfo.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>User Info</comment>
<entry key="age">38</entry>
<entry key="password">india@123</entry>
<entry key="username">sachin</entry>
</properties>


package com.usr.properties;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

public class ReadPropertiesXmlFile {
       public static void main(String[] args) {
              Properties pr = new Properties();
              try {
                     InputStream is = new FileInputStream("userinfo.xml");
                     pr.loadFromXML(is);
                     is.close();

                     Enumeration enumKeys = pr.keys();
                     while (enumKeys.hasMoreElements()) {
                           String key = (String) enumKeys.nextElement();
                           String value = pr.getProperty(key);
                           System.out.println(key + ":" + value);
                     }
              } catch (FileNotFoundException ex) {
                     ex.printStackTrace();
              } catch (IOException ex) {
                     ex.printStackTrace();
              }
       }
}
OUTPUT
age:38
password:india@123
username:sachin
 

java program to convert properties file into xml file

userinfo.properties

username=sachin
password=india@123
age=38

package com.usr.properties;

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

public class PropertiesToXML {
       public static void main(String[] args) {
              File file=new File("userinfo.properties");
              try{
                     FileInputStream fis=new FileInputStream(file);
                     BufferedReader br=new BufferedReader(new InputStreamReader(fis));
                     Properties pr=new Properties();
                     pr.load(br);
                     OutputStream os=new FileOutputStream("userinfo.xml");
                     pr.storeToXML(os,"User Info", "UTF-8");
                     System.out.println("converted to xml");
                     br.close();
                     fis.close();
                     os.close();
              }
              catch(FileNotFoundException ex){
                     ex.printStackTrace();
              }
              catch (IOException ex) {
                     ex.printStackTrace();
              }

       }
}

OUTPUT
userinfo.xml 
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>User Info</comment>
<entry key="age">38</entry>
<entry key="password">india@123</entry>
<entry key="username">sachin</entry>
</properties>


Saturday 3 March 2012

java program to find second largest number in array


package com.usr.arrays;

public class SecondLargestNumberInArray {
       public static void main(String[] args) {
              int arr[] = { 1, 23, 47, 81, 92, 88, 52, 48, 56, 66, 65, 76, 71, 85,
                           49, 53, 56, 61, 65, 84 };
              secondLargeNumber(arr);
       }

       public static void secondLargeNumber(int[] arr) {
              int largest = arr[0];
              int secondLargest = arr[0];
              for (int i = 0; i < arr.length; i++) {
                     if (arr[i] > largest) {
                           secondLargest = largest;
                           largest = arr[i];
                         
                     } else if (arr[i] > secondLargest) {
                           secondLargest = arr[i];
                         
                     }
              }
              System.out.println("second largest in array is:" + secondLargest);

       }
}

Saturday 25 February 2012

How to create immutable classes in java


Immutable class is the class whose value cannot be changed throughout the life cycle.
Ex:String
We cannot change the content of String class, everytime new reference is created when we change the content.

To create custom immutable  class in java
  1. Class should be made final so that no class can extend it. 
  2. Declare all variables as final
  3. Provide constructors to set the values
  4.  There should not be any public set method which can change the state of 
    the object.
  5. provde getter method




package com.usr.general;

final class Person {
       final int age;
       final String name;

       public Person(int age, String name) {
              this.age = age;
              this.name = name;

       }

       public int getAge() {
              return age;
       }

       public String getName() {
              return name;
       }
}

public class ImmutableDemo {
       public static void main(String[] args) {
              Person p = new Person(38, "sachin");
              System.out.println(p.age);
              System.out.println(p.name);
       }
}

OUTPUT
38
sachin

Friday 24 February 2012

java program to convert char array to String


package com.usr.strings;

public class ConvertCharArrayToString {
       public static void main(String[] args) {
              char[] charArr = { 'w', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 't', 'o',
                           ' ', 'j', 'a', 'v', 'a', '1', '1', 's' };

              String str = new String(charArr);
              System.out.println("converted String is:" + str);

              String str1 = String.valueOf(charArr);
              System.out.println("covnverted String is:" + str1);
       }
}

OUTPUT
converted String is:welcome to java11s
covnverted String is:welcome to java11s