Monday, November 14, 2011

Using Properties in Java

Properties file is being used commonly in every project to get external or supported parameters like configurations, urls, port numbers and ids etc. We can handle these files using file stream APIs like FileInputStream, FileOutputStream etc, provided in java by treating these files as normal text files. But another way is also there to handle the property files, which is the best and efficient way to handle these by using the API java.util.Properties.

The class Properties has few methods on which the below are very important and most useful.

  1. load(InputStream in): To load the external properties file
  2. loadFromXML(InputStream in): To load an XML file as a property file
  3. getProperty(String key): To read value of a property key from the property file
  4. getProperty(String key,String defaultValue): To read value of a property key from the property file. If the key doesn’t exist then it will return the defaultValue.
Let us see an example to understand how to use the Properties class.
Create a properties file which contains keys separated by new line and their corresponding value pairs.
Below is a sample properties file which has three properties “name”, “message.ok” and “message.error”.
message.ok=You are a valid user
message.error=You are not valid user

Let us write a java program to use these properties.
Below is a sample program which reads the property “name” from the property file and if the comparison is true then it will print the value of the property “message.ok” else prints the value of the key “message.error”.
Learning Mela, author: Anand Y

Java code: PropertiesTest.javaimport java.util.Properties;
public PropertiesTest
String propFile="";
public static void main(String[] arg)
Properties props = new Properties();

The below statement is to load the property file from the path where the .class file of the above program is existed.

Visit articles page of the website for more details.

Thursday, November 10, 2011

Write Files

WriteFiles is a java tool to integrate several textual files to a single file. In some scenarios like where we need to copy all source code files into a document or a textual file, it is difficult to copy the content of all the files manually. For example, if you take java application where there will be different source files under different packages, it starts from a root directory (package), includes sub directories and their sub directories and so on. Each directory or sub directory may have their individual java classes. If you want to copy all these source files including the sub folders, think how difficult it is to do so. This tool can help you to do the same.

Visit Projects page of for more details and to download the tool.

Wednesday, November 2, 2011


This is a runtime error simply means that the JVM ran out of memory. 

We can choose either of the below two solutions to fix it.

1)      Prevent memory leak: Yes. Preventing memory leak will also improve the performance of the application. A memory leak can be very latent. A memory leak occurs when the application creates more references to objects and never releases them. So the garbage collector will never collect those objects results less free memory will be available, hence not enough free memory is available for the application to function normally. 

2)      Increase the heap size of the JVM.
It can be done by using the VM argument –Xmx 

Syntax: java -Xmx<size> <java class> -d<bit mode>

Where, Size is the size of the memory asking JVM to take
Java class is the program name to which memory is required
Bit mode tells which bit mode of operating system is being used. Either 32 bit or 64 bit 

Ex: java -Xmx1024m MyJavaClass -d32 //It asks JVM for 1024 MB space Java
      java -Xmx2g MyJavaClass –d32       //It asks JVM for 2 GB space Java

Visit articles page of the website for more details.