Sunday, May 20, 2012

Java : system / Environment properties


There was a need to determine the OS on which my application is running. So come across system properties method in java. Sharing the same with you..

/**
       * I could able to get below properties..!!
       
  java.runtime.name, sun.boot.library.path, java.vm.version, java.vm.vendor,
  java.vendor.url,  path.separator, java.vm.name, file.encoding.pkg,
  user.country, user.script, sun.java.launcher,  sun.os.patch.level,
  java.vm.specification.name, user.dir, java.runtime.version,
  java.awt.graphicsenv,  java.endorsed.dirs, os.arch, java.io.tmpdir,
  line.separator, java.vm.specification.vendor, user.variant,
  os.name, sun.jnu.encoding, java.library.path, java.specification.name,
  java.class.version,  sun.management.compiler, os.version, user.home,
  user.timezone, java.awt.printerjob, file.encoding,
  java.specification.version, java.class.path, user.name,
  java.vm.specification.version, sun.java.command,  java.home,
  sun.arch.data.model, user.language, java.specification.vendor,
  awt.toolkit, java.vm.info,  java.version, java.ext.dirs,
  sun.boot.class.path,
  java.vendor, file.separator,  java.vendor.url.bug, sun.io.unicode.encoding,
  sun.cpu.endian, sun.desktop, sun.cpu.isalist
       
       */
      @SuppressWarnings("rawtypes")
      private static void printSystemProperties(){
            Properties properties=System.getProperties();
            String key;
            Enumeration keys=properties.keys();
            while(keys.hasMoreElements()){
                  key=(String)keys.nextElement();
                  System.out.println("\n");
                  System.out.println("key :"+key);
                  System.out.println("value :"+properties.getProperty(key));
            }
      }
Happy coding :)

Tuesday, May 3, 2011

String Tokenizer in Java II

Refer the first post on String Tokenizer here.

Lets play with a little complicated string in this post! This will just demonstrate how you can use Tokenizer in more often encountered string parsing situations.

Lets run....



package org.chandan.string.tokenizer;

import java.util.StringTokenizer;

public class StringTokenizerSample {

/*
* Notice that, first we need to tokenize with respect to ';' as
* delimiter.
* Then on generated tokens, we have to again tokenize
* with '=' as delimiter.
*/
private static final String SAMPLE_STRING_1=
    "name=myName;address=myAddress;phone=myPhone";

private static final String DELIM_EQUAL="=";

private static final String DELIM_SEMICOLON=";";
 
public static void main(String args[])
{
       System.out.println("PROCESSING STRING =     ["+SAMPLE_STRING_1+"]");
       System.out.println("----------------------------------------");
       processString(SAMPLE_STRING_1,DELIM_SEMICOLON,DELIM_EQUAL);
    System.out.println("");
    System.out.println("*********************************************");
}

private static void processString(String sample,String     delimiter1
,String delimiter2)
{
/*
* First tokenize whole string with respect to first delimeter.
* Then iterate over generated tokens to perform further tokenization.
*/
    StringTokenizer stringTokenizer=new    StringTokenizer(sample,delimiter1);
    System.out.println("TOTAL NO OF MAIN TOKENS FOUND: "
   +stringTokenizer.countTokens());
    int tokenCount=1;
   while(stringTokenizer.hasMoreElements())
   {
     String mainToken=stringTokenizer.nextElement().toString();
     System.out.println("");
     System.out.println("MAIN TOKEN["+tokenCount++ +"] -> "
+mainToken);
     System.out.println(".................................");
    StringTokenizer stringTokenizer2=new StringTokenizer(
mainToken,delimiter2);
    System.out.println("");
    System.out.println("TOTAL NO OF SUB TOKENS FOUND: "
    +stringTokenizer2.countTokens());
    int subTokenCount=1;
    while (stringTokenizer2.hasMoreElements())
    {
        System.out.println("SUB TOKEN["+subTokenCount++ +"] -> "
        +stringTokenizer2.nextElement());
     }//END OF INNER WHILE LOOP
   }//END OF OUTER WHILE LOOP
  }//END OF METHOD
}



OUTPUT:

PROCESSING STRING = [name=myName;address=myAddress;phone=myPhone]
----------------------------------------
TOTAL NO OF MAIN TOKENS FOUND: 3

MAIN TOKEN[1] -> name=myName
.................................

TOTAL NO OF SUB TOKENS FOUND: 2
SUB TOKEN[1] -> name
SUB TOKEN[2] -> myName

MAIN TOKEN[2] -> address=myAddress
.................................

TOTAL NO OF SUB TOKENS FOUND: 2
SUB TOKEN[1] -> address
SUB TOKEN[2] -> myAddress

MAIN TOKEN[3] -> phone=myPhone
.................................

TOTAL NO OF SUB TOKENS FOUND: 2
SUB TOKEN[1] -> phone
SUB TOKEN[2] -> myPhone

*********************************************

If you are looking for some tutorials on parsing an xml document, then check HERE.


Happy coding :)



.