Java
  Home arrow Java arrow Page 2 - Reading a Properties File for a Java Appli...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Reading a Properties File for a Java Application using NetBeans IDE
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 11
    2006-05-31

    Table of Contents:
  • Reading a Properties File for a Java Application using NetBeans IDE
  • How to get the current working folder (or where the current class file exists) in Java
  • Starting to develop a Java application using NetBeans IDE
  • Adding a properties file
  • Reading the connection string from the properties file

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Reading a Properties File for a Java Application using NetBeans IDE - How to get the current working folder (or where the current class file exists) in Java


    (Page 2 of 5 )

    The previous section dealt with retrieving the current class file name (along with the class name).  I would like to utilize it here to get the class file name first, and then extend it to get the current working folder.

    Let us go through the following code first:

    public String getLocalDirName()
       {
          String localDirName;     

          //Use that name to get a URL to the directory we are
    executing in
          java.net.URL myURL = this.getClass().getResource
    (getClassName());  //Open a URL to the our .class file     

          //Clean up the URL and make a String with absolute path
    name
          localDirName = myURL.getPath();  //Strip path to URL object
    out
          localDirName = myURL.getPath().replaceAll("%20", "
    ");  //change %20 chars to spaces     

          //Get the current execution directory
          localDirName = localDirName.substring
    (0,localDirName.lastIndexOf("/"));  //clean off the file
    name     

          return localDirName;
       }

    I shall explain the above program part by part.  Let us first consider the following:

    java.net.URL myURL = this.getClass().getResource(getClassName
    ());
     

    You can observe that I am using the method "getClassName()" in the previous section.  When the statement gets executed, it gives us the entire execution path of the current class in the form of an URL (including the class name and other URL stuff).  Now, we need to extract the exact path (only the folder information) and return the same to the calling program.  Further proceeding,we have the following:

    //Clean up the URL and make a String with absolute path name
    localDirName = myURL.getPath();  //Strip path to URL object out
    localDirName = myURL.getPath().replaceAll("%20", " ");  //change
    %20 chars to spaces 

    The above statements simply clean out the URL stuff.  Every URL shall have some characters encoded (such as spaces).  In the above statement, I only took care of the spaces.  If your path includes any other encoded characters, you need to replace them accordingly.  Further proceeding, we have the following:

    //Get the current execution directory
          localDirName = localDirName.substring
    (0,localDirName.lastIndexOf("/"));  //clean off the file name

    The above would simply trim off anything which is after the last folder slash (in this case it is the file name), resulting in only the folder name and its path!  We finally return the same to the calling function by issuing the following statement.

    return localDirName;

    The upcoming sections will provide you with few step-by-step guidelines for dealing with the ".properties" file in Java.

    More Java Articles
    More By Jagadish Chaterjee


       · Hello guys! You can read properties file in a simpler fashion using Java together...
       · ResourceBundle bundle =...
       · Your method getLocalDirName can be improved in three ways:1) you call...
     

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT