Home arrow Java arrow Reading a Properties File for a Java Application using NetBeans IDE

Reading a Properties File for a Java Application using NetBeans IDE

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.

Author Info:
By: Jagadish Chaterjee
Rating: 5 stars5 stars5 stars5 stars5 stars / 33
May 31, 2006
  1. · Reading a Properties File for a Java Application using NetBeans IDE
  2. · How to get the current working folder (or where the current class file exists) in Java
  3. · Starting to develop a Java application using NetBeans IDE
  4. · Adding a properties file
  5. · Reading the connection string from the properties file

print this article

Reading a Properties File for a Java Application using NetBeans IDE
(Page 1 of 5 )

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.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials