Reading a Properties File for a Java Application using NetBeans IDE
(Page 1 of 5 )
This article introduces you to a step-by-step process for developing Java (or JFC) based applications with Microsoft SQL Server as the database, using NetBeans IDE. It is the second in a series, and focuses on showing you how to read the properties file.
A downloadable file for this article is available
here.
I already introduced you to the NetBeans IDE in my earlier article, "Developing Java Applications using NetBeans." Even though that article is a fairly introductory piece, the next two articles concentrate on basics of JFC. You can find those articles here and here.
If you are new to NetBeans IDE, I strongly suggest you go through the previous articles first, before proceeding with this one. If you are new to developing Microsoft SQL Server based Java applications, I suggest you go through another article of mine here.
How to get the current class name of the Java application being executed
Before directly going into how you read the properties file (for the connection string), we need to have some "utility" methods which may be necessary in certain scenarios. One of those utilities is the "CLASSNAME." In this section, we shall discuss a small "utility" method for retrieving the current class name.
Let us first go through the following code:
public String getClassName()
{
String thisClassName;
//Build a string with executing class's name
thisClassName = this.getClass().getName();
thisClassName = thisClassName.substring
(thisClassName.lastIndexOf(".") + 1,thisClassName.length());
thisClassName += ".class"; //this is the name of the
bytecode file that is executing
return thisClassName;
}
Within the above method, the following statement already retrieves the class name we want:
thisClassName = this.getClass().getName();
But, the above statement may also retrieve the package name (or other information). So, the next statement extracts only the pure "class" name (by simply reading the class name after the final dot):
thisClassName = thisClassName.substring(thisClassName.lastIndexOf
(".") + 1,thisClassName.length());
At this point, it should be more than enough. Last but not least, I would also like to have the class name be turned to "class file name" (by simply adding the extension ".class" at the end). The following statement does the same:
thisClassName += ".class"; //this is the name of the bytecode
file that is executing
Once everything is ready, we return it to the calling function by issuing the following statement:
return thisClassName;
The next section will deal with retrieving the current folder where the class file exists. If you are not interested, you can skip to the next section, but please be aware that I am using all the utility methods in this application to achieve what we require.
Next: How to get the current working folder (or where the current class file exists) in Java >>
More Java Articles
More By Jagadish Chaterjee