[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 {
            param1 = prop.getProperty("initYears1").trim();
            param2 = prop.getProperty("initYears2").trim();
        } catch (IOException e) {

     *  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[]){

Run Results:

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

分类:Java 时间:2010-03-31 人气:268
blog comments powered by Disqus


  • java.lang.NoClassDefFoundError: javax.swing.tree.TreeNode is a restricted class 2010-09-13

    Today in the GAE configure Struts in a lot of problems, the following is one of the Problem accessing /hello.action. Reason: java.lang.NoClassDefFoundError: javax.swing.tree.TreeNode is a restricted class. Please see the Google App Engine developer's

  • Inside java.lang.Enum 2010-08-04

    1 Introduction to enum Java SE 5 introduced the enumeration, and add a new keyword enum. Here is a enumeration of examples: public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES; } Enumeration type is an ordinary Java class, inherited from the java.lang

  • java.lang包概述(5) 2012-03-09

    java.lang.Exception 根异常.用以描述应用程序希望捕获的情况. java.lang.IllegalAccessException 违法的访问异常.当应用试图通过反射方式创建某个类的实例.访问该类属性.调用该类方法,而当时又无法访问类的.属性的.方法的或构造方法的定义时抛出该异常. java.lang.IllegalMonitorStateException 违法的监控状态异常.当某个线程试图等待一个自己并不拥有的对象(O)的监控器或者通知其他线程等待该对象(O)的监控器时,抛出

  • java.lang.RuntimeException: Non matching type for inject of field: 2010-01-21

    20:37:56,015 ERROR [STDERR] java.lang.RuntimeException: Non matching type for inject of field: private ejbModule.persistence.userAdmin.user.UserDao ejbModule.business.apply.DeptTempMgrBean.userDao for type: $ Proxy96 of jndiName env / ejbModule.busin

  • Tomcat exception of java.lang.IllegalArgumentException: Document base *** does not exist 2010-03-26

    Tomcat exception of java.lang.IllegalArgumentException: Document base Some have just started using Tomcat friend problems, clearly has a web application from tomcat webapps down under the addition, why would it throw this exception? java.lang.Illegal

  • 使用dspmqver -p7时报java.lang.NoClassDefFoundError错误,如何解决? 2012-03-09

    环境:产品: WebSphere MQ 平台: 平台无关 软件版本: v6 问题: 使用dspmqver -p7时报java.lang.NoClassDefFoundError错误,如何解决? dspmqver -p7结果如下: Name: WebSphere MQ Version: CMVC level: p000-L050519 BuildType: IKAP - (Production) Exception in thread "main" java.lang.N

  • Java 内存溢出(java.lang.OutOfMemoryError)的常见情况和处理方式总结 2015-01-20

    java.lang.OutOfMemoryError这个错误我相信大部分开发人员都有遇到过,产生该错误的原因大都出于以下原因:JVM内存过小.程序不严密,产生了过多的垃圾. 导致OutOfMemoryError异常的常见原因有以下几种: 内存中加载的数据量过于庞大,如一次从数据库取出过多数据: 集合类中有对对象的引用,使用完后未清空,使得JVM不能回收: 代码中存在死循环或循环产生过多重复的对象实体: 使用的第三方软件中的BUG: 启动参数内存值设定的过小: 此错误常见的错误提示: tomcat

  • could not bind value '25732 'to parameter: 1; java.lang.String 2011-01-18

    Today in the background to add a proxy feature: the abolition of the domain name transferred. Encountered in the process of writing an exception, the information is as follows: could not bind value '25732' to parameter: 1; java.lang.String The reason

  • String java.lang.String.intern()的作用 2013-05-10

    NOTE:intern方法是native方法 String java.lang.String.intern() Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already

  • java.lang.String的Intern()方法的利弊 2014-09-09

    Java6 API public String intern() Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a string equal

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.232 (s). 10 q(s)