Tuesday, September 15, 2009

Program to check two object of the same pojo same class having same valur or not

//This is Pojo class with two property name and email
class PojoBean {
private String name;
private String email;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}


}
// main class to test pojo object
class PojoMain
{
public static void main(String[] args)
{
//create first instance of the Pojo Class and set property values.
PojoBean pBeanOne = new PojoBean();
pBeanOne.setName("Pushpa");
pBeanOne.setEmail("ashish@gmail.com");


//create second instance of the Pojo Class and set property values.
PojoBean pBeanTwo = new PojoBean();
pBeanTwo.setName("ashish");
pBeanTwo.setEmail("ashish@gmail.com");

//code to check two object is having same data or not.
if(pBeanOne.getName().equals(pBeanTwo.getName())){
System.out.println("Two object is having same Name");
}
// second property email
if(pBeanOne.getEmail().equals(pBeanTwo.getEmail())){
System.out.println("Two object is having same email");
}



}
}

Read data from console and print in one file.

//Read data from console and print in one file.
import java.io.*;
class ReadDataFromConsole
{
public static void main(String[] args)
{
try{
String fileStr = ""; // declare a string variable
for(int i = 0; i'<'args.length; i++){ // for loop start with argument size

fileStr = fileStr + " " + args[i];
}


//create an instance of the random acces file "rfile'.
RandomAccessFile rfile = new RandomAccessFile("C:/Temp/text.txt","rw");
// write object into the file.
rfile.writeBytes(fileStr);


}catch(Exception ex){
ex.printStackTrace();

}
}
}