May 19, 2007

java: Using ClassLoader to read a properties file in classes

Properties properties = null;
  String CONFIGURETIONFILE = "com/travelsky/titi/common/GlobalParameters.properties";
  if (properties == null) {
   try {
    properties = new Properties();
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    InputStream inputStream = null;
    if (cl != null) {
     inputStream = cl.getResourceAsStream(CONFIGURETION
FILE);
    }
    if (inputStream != null) {
     properties.load(inputStream);
     inputStream.close();
     //set database parameters
     DbUtils.databaseType = Integer.parseInt(properties.getProperty("database.databaseType"));
     DbUtils.driverClassName = properties.getProperty("database.driverClassName");
     DbUtils.url = properties.getProperty("database.url");
     DbUtils.username = properties.getProperty("database.username");
     DbUtils.password = properties.getProperty("database.password");
     //set uploadFileDir
     GlobalParameters.uploadFileDir=properties.getProperty("uploadFileDir");
     //set userId and password
     GlobalParameters.userId=properties.getProperty("userid");
     GlobalParameters.password=properties.getProperty("password");
     
     System.out.println("Getting global parameters successfully!");
    }
    
   } catch (Exception e) {
    e.printStackTrace();
    System.out.println("ERRORS:error on getting global parameters!");
   }

Comments »

The URI to TrackBack this entry is: http://tomshen.blogsome.com/2007/05/19/java-using-classloader-to-read-a-properties-file-in-classes/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>