Saturday, March 24, 2012

How To Validate Email Address in Java Mail API

1 comments

Validate Email Address using Java Mail API


In this tutorial you will find how to validate email address using java Mail API. You must to import javax.mail.internet.InternetAddress 



package jsupport;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

/**
 *
 * @author Jsupport http://javasrilankansupport.blogspot.com
 */
public class ValidateEmailAddress {

    public static  boolean  validateEmail(String email) {
        try {
            new InternetAddress(email).validate();

        } catch (AddressException ex) {
            System.out.println("Error : " + ex.getMessage());
            return false;
        }
        return true;
    }

    public static void main(String[] args) {

        validateEmail("info@jsupport.com");

    }
}

Read more...

Thursday, March 22, 2012

How to Create Directory in Java,Directory Creation in Java

0 comments

Java Create Directory - Java Tutorial


In this Tutorial you will learn how to create directory and check whether that directory is exist.If the directory is exist then it will not create otherwise,


import java.io.File;

/**
 *
 * @author JSupport http://javasrilankansupport.blogspot.com
 */
public class CreateDirectory {

    public static void main(String[] args) {

        String directory = "JAVADIR";
        String directory_tree = "JSUPPORT/JAVA";
        boolean dir_exist = false;

        try {
            // Check whether the derectory is exist
            dir_exist = new File(directory).isDirectory();

            if (dir_exist) {
                System.out.println("directory already created.....");
            } else {
                // creat directory
                new File(directory).mkdir();
            }

            // Check whether the derectory is exist
            dir_exist = new File(directory_tree).isDirectory();

            if (dir_exist) {
                System.out.println("directories already created.....");
            } else {
                // create directories
                new File(directory_tree).mkdirs();
            }

        } catch (Exception e) {
            System.out.println("Error : "+e.getMessage());
        }

    }
}

Read more...

How to Write a File in Java,Java Write to File Example,Write a File in Java

0 comments

Java a Write file  - Java Tutorial

In this Tutorial you will learn how to write a file,It use FileWriter class and BufferedWriter class to write the file.



import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 *
 * @author JSupport http://javasrilankansupport.blogspot.com
 */
public class WriteFile {


    public static void writToFile(String data){

        try {

            // Create a new file  TEST.txt
            FileWriter file_writer  = new FileWriter("TEST.txt");
            BufferedWriter bufferd_writer   =   new BufferedWriter(file_writer);

            // write data to file
            bufferd_writer.write(data);
            // flush and close the file
            bufferd_writer.flush();
            bufferd_writer.close();

        } catch (IOException ex) {
            System.out.println("Cannot write    :" + ex.getMessage());
        }
    }

    public static void main(String[] args) {
        writToFile("Hello Java Sri Lankan Support");
    }
}

Read more...

Wednesday, March 21, 2012

How To Get Date Range in Java Calendar's set() add() and roll()

0 comments

 Calendar (Java 2 Platform SE 5.0) set() add()  and roll() 



import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author JSupport http://javasrilankansupport.blogspot.com
 */

enum DateEnum {
    DAILY,
    WEEKLY,
    MONTHLY;
}

public class DateRange {

    public int x = 2;
    public String date,stdate,endate;

    private String getDateTime(DateEnum type) {

        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Date d = c.getTime();
        
        c.setTime(d);

        switch (type) {

            case DAILY:
                c.roll(Calendar.DAY_OF_WEEK, -1);
                date = sdf.format(c.getTime());
                break;
            case WEEKLY:
                 c.roll(Calendar.DAY_OF_WEEK, -1);
                 stdate = sdf.format(c.getTime());
                 c.roll(Calendar.WEEK_OF_MONTH,-1);
                 endate = sdf.format(c.getTime());
                 date = stdate+","+endate;
                break;

            case MONTHLY:
                 c.roll(Calendar.DAY_OF_WEEK, -1);
                 stdate = sdf.format(c.getTime());
                 c.roll(Calendar.MONTH,-1);
                 endate = sdf.format(c.getTime());
                 date = stdate+","+endate;
                break;
        }
        return date;
    }

    public static void main(String[] arr) {

        DateRange dr = new DateRange();

        System.out.println(dr.getDateTime(DateEnum.DAILY));
        System.out.println(dr.getDateTime(DateEnum.WEEKLY));
        System.out.print(dr.getDateTime(DateEnum.MONTHLY));

    }
}

Read more...

Wednesday, March 14, 2012

Insert and Retrieve Values from a Map (HashMap)

0 comments
  • Insert values into HashMap
  • Retrieve values from HashMap

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
*
* @author JSupport
* http://javasrilankansupport.blogspot.com
*
*/

public class InsertRetrieveDataFromMap {

// get values from HashMap
public static void getHashMapData(Map order_details){
Set key_set;

// gat all key to Set
key_set     =   order_details.keySet();

for (Iterator it = key_set.iterator(); it.hasNext();) {

String key      =   (String) it.next();
String  value   =   (String) order_details.get(key);

System.out.println("Map key : "+key +"\tKey Value : "+key);
}

}
public static void main(String[] args) {

HashMap order_details   =   new HashMap();

//     add key value pairs to HashMap

order_details.put("kitchName", "Chinese Kitchen");
order_details.put("waiterName", "Silva");
order_details.put("tableName", "20");

getHashMapData(order_details);
}
}




This program dose answer to following....
  • How to Insert values into HashMap in Java
  • How to Insert values into HashMap in Java
  • How to Retrieve values from HashMap in Java
  • How to Retrieve values from Map in Java
Read more...

Thursday, March 8, 2012

Get Available COM Ports in Java Comm

0 comments
First of all you must properly install Java Comm API.There is nothing much more




package jsupport.com;

import gnu.io.CommPortIdentifier;
import java.util.Enumeration;

/**
 *
 * @author JSupport
 */
public class GetAvailableComPorts {

    public static void getComPorts(){
        String     port_type;
        Enumeration  enu_ports  = CommPortIdentifier.getPortIdentifiers();

        while (enu_ports.hasMoreElements()) {
            CommPortIdentifier port_identifier = (CommPortIdentifier) enu_ports.nextElement();

            switch(port_identifier.getPortType()){
                case CommPortIdentifier.PORT_SERIAL:
                    port_type   =   "Serial";
                    break;
                case CommPortIdentifier.PORT_PARALLEL:
                    port_type   =   "Parallel";
                    break;
                case CommPortIdentifier.PORT_I2C:
                    port_type   =   "I2C";
                    break;
                case CommPortIdentifier.PORT_RAW:
                    port_type   =   "Raw";
                    break;
                case CommPortIdentifier.PORT_RS485:
                    port_type   =   "RS485";
                    break;
                default:
                    port_type   =   "Unknown";
                    break;
            }

            System.out.println("Port : "+port_identifier.getName() +" Port type : "+port_type);
        }
    }
    public static void main(String[] args) {
        getComPorts();
    }
}

Read more...