Monday, December 29, 2008

Reading A Properties File In Java

How to read a properties file in Java?
Well, here I explain a simple way to read a properties file in Java.

Here is what I have done :
1) I created a folder named 'tests'. Inside this folder I created 2 files.
2) The First file 'PropertyReader.java' will contain the Java source code to read the data from a properties file.
3) The second file named 'SomeProps.properties' is the properties file that needs to be read.

Following are the contents of these two files:

SomeProps.java
name=ryan
interest=programming


PropertyReader.java

//----------Source Code Begins Here------------------------
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;

public class PropertyReader
{
public static void main(String[] args) throws IOException
{
Properties p=new Properties();
p.load(new FileInputStream("SomeProps.properties"));
System.out.println(p.getProperty("name"));
System.out.println(p.getProperty("interest"));
}
}
//-------------------Source Code Ends Here----------------------

The code hardly needs any explanation. All it does is read a properties file using a FileInputStream object and then loading the values into the properties object using the load() method.

Happy programming ;)

Signing Off
Ryan

Sunday, December 28, 2008

ECLIPSE

What is ECLIPSE?
Eclipse is an open source community that is most well known for developing the Eclipse IDE. For those of you who are new to software development, the acronym IDE stands for Integrated Development Environment.

What is an IDE?

To be defined at the lowest level, an IDE lets you develop projects with a lot of code that is self generated by the IDE itself. It helps you to concentrate more on the actual functionality rather than concentrating on good programming practices like indentation/commenting/file organization. For novice developers, such as college students, using an IDE can give you the feel of how softwares are developed in the real word and how they are immaculately organized.


IDE's these days are glutted with a large number of features, such as drag drop, import/export, run/debug, precompilation etc etc. At times these may be confusing to the novice user. However, these features are not intended to obfuscate the user. Most basic functionality and common options are available for use easily and would not take much time to find. It hardly takes a matter of a few minutes or maybe an hour to get started.

Why ECLIPSE?

Eclipse began as early as 2001. However it became a not ofr profit organization only in 2004. It comprises of industry leaders such as IBM, Red Hat, Suse, Rational Software and many more. However from the user point of view, the most compelling reasons for using ECLIPSE are :

1)Open source : So its always freely available.
2)Extremely powerful.
3)Well organized.
4)Average learning curve.
5)Appealing interface and design.
5)Adequate assistance from free tutorials and blogs.
6)Quality Documentation.

ECLIPSE is used everywhere in the industry, so its highly recommended for students to be at par with the industry standards. Moreover, ECLIPSE has such a large user base that you are almost always bound to find someone could resolve your problem and offer suggestions and tricks.

The official eclipse website link is given below.
http://www.eclipse.org/

Happy developing.

Signing off
Ryan

Saturday, December 27, 2008

Setting Decimal Precision

Here is a simple program that is used to set the precision digits in for a floating point number.

This program was created in Eclipse using the jdk 5.0.

//-----------Program code begins here-------------

import java.text.NumberFormat;

public class SetPrecision {

public static void main(String args[])
{
//A sample float number.
float anumber= 100.12345678F;

//Creating a NumberFormat object that will used to
//format a number.
NumberFormat nf= NumberFormat.getInstance();

//Setting the number of digits that are required to be
//printed after the decimal point.
nf.setMaximumFractionDigits(3);

//Formatting the number returns a string object.
String truncatedNumberString=nf.format(anumber);

//Parsing the string into a float using a
//Wrapper class function - Float.parseFloat
float truncatedNumber=Float.parseFloat(truncatedNumberString);

System.out.println(truncatedNumber);

}
}

//-----------Program code Ends here-------------

Signing off
Ryan

Wednesday, December 3, 2008

Java Compilation Problems (2)

Ah well, in this blog I would like to address an issue that had confronted me about 2 years ago. I really had to pull out all my hairs to resolve this problem at that time since I received no external help. But as good as your luck turns out to be, I am just glad to be able to help. Setting classpath variables is a headache for novice programmers. So i'll try to make it as simple as possible.

To execute a java program from any directory in your system, u need to do the following (this is for windows XP users only) :

1) Right click on My Computer->properties
2) Go to the Advanced tab
3) You shall see a small button named Environment variables. That's the one u need to click.
4) A dialog box will open up showing the various system variables.
5) U need to add a new environment variable in the user variable dialog box. Click on new.

Add the following:
Variable name : PATH
Variable value : the complete path to the bin folder of your java installation directory
For example, on my system, the setting is the follows:
Variable name : PATH
Variable value : C:\Program Files\Java\jdk1.5.0_12\bin

There you go. Just go and have a good time playing around with all the java files that u can, and experience the ease with which you can compile the files without setting the classpath each time or painfully copying the files to the bin folder.

Signing off
Ryan

Java Compilation Problems (1)


The problem that I am going to address here was resolved by me by accident. When I used to try to run a java file using java filename , I used to get the error :

NoClassDefinitionFoundError filename
It was absolutely irritating. However after about 18 months of struggle(including re-installing my OS several times in that period) I came up with a proper solution


This thing will most probably work provided that u have the proper classpath set as I have mentioned in my previous post. Or else I hope that it should work anyway.

When running or compiling a file use the following-

javac -cp . Filename.java
java -cp . Filename

or

javac -classpath . Filename.java
java -classpath . Filename


What u actually are doing over here is that you are telling the java compiler to search for files in the current directory also (the “.” stands for the current directory) when it tries to compile or or run a java program.

Signing off
Ryan