We all must have used the properties file for using our desktop\web application in different locales. But once in one of the projects I encountered a situation where we had to write the configuration settings to a properties file which were a one time activity from the end user's side.
We followed the following approach to write to a .properties file from java code
File propFile = new File(System.getProperty("user.dir")+"src\\uts\\config\\envConfig.properties");
Properties prop = new Properties();
try
{
FileInputStream fis = new FileInputStream(propFile);
prop.load(fis);
prop.setProperty("env_"+environmentName, environmentName);
prop.setProperty("uid_"+userId, userId);
prop.setProperty("pass_"+password, password);
prop.store(new FileOutputStream(propFile), null);
}
catch(Exception e)
{
e.printStackTrace();
}
Monday, October 12, 2009
Tuesday, September 29, 2009
Serialization
Serialization is a technique to write object's state to a data file. In order to be able to write the object to a file the class whose instance (object) we need to write has to implement the marker interface Serializable. This interface does not contain any methods.There are two methods that we use to serialize and deserialize an object, they are writeObject(Object) , readObject(). The writeObject method is in the ObjectOutputStream class while the readObject() method is in the ObjectInputStream class. All the instance variables of the object are saved and retrieved successfully as they were saved provided no instance variable is marked with the keyword transient. This keyword if used specifies that the state of that instance variable is not to be saved.
Following code demonstrates how to do serialization
Following code demonstrates how to do serialization
Monday, September 28, 2009
What can be done with enums?
This post shows the various things that enums can do. Enum data type was introduced since Java 5 and is a very powerful type. It is a special type of class which contains constant objects, instance variables,constructors,overloaded constructors, methods and overloaded methods as well. Enums can be declared outside or inside the class.Another important feature of enums is semicolon is optional.
Sunday, September 20, 2009
Calling new on an object
When can we call a new on an object? We normally use a new keyword in java to create a new object as follows:
Integer i = new Integer();
But can we use the new keyword on an object itself as follows:
TestClass.MyClass c = new TestClass().new MyClass();
Integer i = new Integer();
But can we use the new keyword on an object itself as follows:
TestClass.MyClass c = new TestClass().new MyClass();
Wednesday, September 16, 2009
Generics with non - generics
When we use generics we use the syntax as List< Integer > newList= new ArrayList< Integer >(), Can there be a way by which I can put a string into it. How can I cheat....????
Of course there is a way. This is by mixing generic code with non-generic code.
This is how it can be done.
public class TestGeneric{
public static void main(String args[])
{
List newList = new ArrayList ();
newList.add(7); // autoboxing feature
newList.add(new Integer(10)); // normal adding objects
insert(newList);
}
public void insert(List nonGenericList)
{
nonGenericList.add("42"); // valid compiles fine and surprisingly runs fine too.....check the reason y below
}
}
Of course there is a way. This is by mixing generic code with non-generic code.
This is how it can be done.
public class TestGeneric{
public static void main(String args[])
{
List
newList.add(7); // autoboxing feature
newList.add(new Integer(10)); // normal adding objects
insert(newList);
}
public void insert(List nonGenericList)
{
nonGenericList.add("42"); // valid compiles fine and surprisingly runs fine too.....check the reason y below
}
}
Monday, September 14, 2009
Does finally block always execute?
I had always thought that the finally block in a try-catch-finally statement always executes. But this is not true. I found one exceptional case where the finally block does not execute.
Please see the below code example.
public class TestException
{
static int choice = 5;
public static void main(String[] args)
{
try{
if (choice != 5)
{
System.out.println("hello");
}
else
{
System.out.println("In else");
System.exit(1);
}
}
finally
{
System.out.println("in finally");
}
}
}
Please see the below code example.
public class TestException
{
static int choice = 5;
public static void main(String[] args)
{
try{
if (choice != 5)
{
System.out.println("hello");
}
else
{
System.out.println("In else");
System.exit(1);
}
}
finally
{
System.out.println("in finally");
}
}
}
Hashtable and HashMap
In the java's collections framework we have two classes HashMap and Hashtable which implement the map interface.Both of them perform the same function of storing data in key - value pair, but Hashtable was older version of HashMap. Hashtable is synchronized means all the methods in the Hashtable class are synchronized i.e they are thread safe. Also if there is a difference in the naming convention, notice that the 't' is in lower case but the 'M' in HashMap is upper case following the camel case convention. HashMap is faster for iteration as compared to Hashtable as it is not synchronised. It is recommended to use HashMap unless you have a very good reason to use Hashtable.
Another point of difference is that HashMap can have multiple null values but the keys of those values should be different. Also there can exist one null key in the HashMap but there cannot be any null keys or even values for that matter in a Hashtable.
Another point of difference is that HashMap can have multiple null values but the keys of those values should be different. Also there can exist one null key in the HashMap but there cannot be any null keys or even values for that matter in a Hashtable.
Subscribe to:
Posts (Atom)