[Reprint] Java Reflection to read properties file

Java read more properties of documents, most of the articles online I was "Java properties file to read the six methods", but in Java applications, the most common or through java.lang.Class class getResourceAsStream (String name) methods to achieve, but I see many properties file to read the code, will to do this:

InputStream in = getClass (). GetResourceAsStream ("Resource Name");

Inside there is a problem that is getClass () is called when the default omitted this! We all know, this is not static (static) method or the static block in use, because the static type of method or block of code belongs to class itself, not an object, but this in itself represents the current object, static method or block calls when the object is not initialized.

The question is: If I do not want a class of objects, then I will set such a default constructor private, of course, there would not write another constructor. And my class is the tools are static methods and variables, I would like to block or in a static method to obtain the static properties file, this method is not.

Then how should we do? In fact, this class is not so used, he just needs to obtain a Class object on it, it also Burongyia - take all classes of the parent class Object, it not better to use Object.class with you are writing your own class facilitate the safe? Oh, an example is given below, to facilitate communication.

import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;

/**
*  Read examples of Properties file  
* File: TestProperties.java
* User: leizhimin
* Date: 2008-2-15 18:38:40
*/
public final class TestProperties {
    private static String param1;
    private static String param2;

    static {
        Properties prop = new Properties();
        InputStream in = Object.class.getResourceAsStream("/test.properties");
        try {
            prop.load(in);
            param1 = prop.getProperty("initYears1").trim();
            param2 = prop.getProperty("initYears2").trim();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  A private constructor method, do not need to create an object  
     */
    private TestProperties() {
    }

    public static String getParam1() {
        return param1;
    }

    public static String getParam2() {
        return param2;
    }

    public static void main(String args[]){
        System.out.println(getParam1());
        System.out.println(getParam2());
    }
}


Run Results:

151
152 of
Process finished with exit code 0

Of course, the Object.class replaced int.class did, and Oh, we can try.

In addition, if yes static method or block read Properties files, there is a safest way is the class of its own to directly obtain the Class object name, such as in this case can be written TestProperties.class, this is the safest method.
This article comes from " lava "blog, be sure to keep this source http://lavasoft.blog.51cto.com/62575/62174
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of [Reprint] Java Reflection to read properties file

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • The level Hibernate cache

    Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...

  • Great collection of java interview topics

    1, object-oriented features of what has 1. Abstract: Abstract is that it has overlooked a subject has nothing to do with the current goal of those aspects in order to more fully with the current objectives of the attention-related aspects. Abstract does n

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries