[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

分类:Java 时间:2010-03-31 人气:250
分享到:
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.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

  • 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

  • an important class java.lang review, reflection and thread 2010-04-05

    java.lang package is a Java-based package, at compile time, java.lang package is automatically imported source files. contains the Object class and java.lang wrapper classes (Boolean, Character, Byte, Short, Integer, Long, Float and Double) and so on

  • Solution - java.lang.OutOfMemoryError: unable to create new native thread 2010-03-23

    Work, come across this issue several times, I feel it is necessary to sum up, so has this article, this article is divided into three parts: understanding, analyzing and solving problems. One to know the problems: First, we test through the following

  • Exception in thread main java.lang.unsupportedclassversion exception 2010-04-02

    On the final design of Rights Since it is so image watermarking algorithm, we must first realize that the image read and write friends. Write simple little demo import java.io.*; import javax.imageio.*; import java.awt.image.*; public class demo { pu

  • java.lang.reflect dynamic proxy 2010-04-25

    public interface IReport { String published(); } public class Report implements IReport{ public String published() { System.out.println("hello~~~"); return "return Hello"; } } import java.lang.reflect.InvocationHandler; import java.lan

  • About java.lang.NoClassDefFoundError: com / sun / mail / util / LineInputStream 2010-06-05

    (Based on MyEclipse6.5 other IDE I do not know) Grateful black hair, (some people says, black hair, short black hair ....), old black bamboo, Sun yujia Receive e-mail: Exception in thread "main" java.lang.NoClassDefFoundError: com / sun / mail /

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.554 (s). 12 q(s)