`
tangbinghn
  • 浏览: 13159 次
  • 性别: Icon_minigender_2
  • 来自: 长沙
社区版块
存档分类
最新评论

使用J2SE API读取Properties文件的六大方法(综合篇)

阅读更多
1.使用java.util.Properties类的load()方法

示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));

Properties p = new Properties();

p.load(in);

2.使用java.util.ResourceBundle类的getBundle()方法

示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

3.使用java.util.PropertyResourceBundle类的构造函数

示例: InputStream in = new BufferedInputStream(new FileInputStream(name));

ResourceBundle rb = new PropertyResourceBundle(in);

4.使用class变量的getResourceAsStream()方法

示例: InputStream in = JProperties.class.getResourceAsStream(name);

Properties p = new Properties();

p.load(in);

5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法

示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);

Properties p = new Properties();

p.load(in);

6.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法

示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);

Properties p = new Properties();

p.load(in);

补充

Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法

示例:InputStream in = context.getResourceAsStream(path);

Properties p = new Properties();

p.load(in);

完整的示例:

JProperties.java文件


package com.kindani;

//import javax.servlet.ServletContext;
import java.util.*;
import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;

/**
* 使用J2SE API?取Properties文件的六种方法
* User: SYNFORM
* Date: 2005/07/12
* Time: 18:40:55
* To change this template use File | Settings | File Templates.
*/
public class JProperties {

    public final static int BY_PROPERTIES = 1;
    public final static int BY_RESOURCEBUNDLE = 2;
    public final static int BY_PROPERTYRESOURCEBUNDLE = 3;
    public final static int BY_CLASS = 4;
    public final static int BY_CLASSLOADER = 5;
    public final static int BY_SYSTEM_CLASSLOADER = 6;

    public final static Properties loadProperties(
final String name, final int type) throws IOException {
        Properties p = new Properties();
        InputStream in = null;
        if (type == BY_PROPERTIES) {
            in = new BufferedInputStream(new FileInputStream(name));
            assert (in != null);
            p.load(in);
        } else if (type == BY_RESOURCEBUNDLE) {
            ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
            assert (rb != null);
            p = new ResourceBundleAdapter(rb);
        } else if (type == BY_PROPERTYRESOURCEBUNDLE) {
            in = new BufferedInputStream(new FileInputStream(name));
            assert (in != null);
            ResourceBundle rb = new PropertyResourceBundle(in);
            p = new ResourceBundleAdapter(rb);
        } else if (type == BY_CLASS) {
            assert (JProperties.class.equals(new JProperties().getClass()));
            in = JProperties.class.getResourceAsStream(name);
            assert (in != null);
            p.load(in);
            //return new JProperties().getClass().getResourceAsStream(name);
        } else if (type == BY_CLASSLOADER) {
            assert (JProperties.class.getClassLoader().equals(
new JProperties().getClass().getClassLoader()));
            in = JProperties.class.getClassLoader().getResourceAsStream(name);
            assert (in != null);
            p.load(in);
            //return new JProperties().getClass().
getClassLoader().getResourceAsStream(name);
        } else if (type == BY_SYSTEM_CLASSLOADER) {
            in = ClassLoader.getSystemResourceAsStream(name);
            assert (in != null);
            p.load(in);
        }

        if (in != null) {
            in.close();
        }
        return p;

    }

    // ---------------------------------------------- servlet used
/*
    public static Properties loadProperties(
ServletContext context, String path) throws IOException {
        assert (context != null);
        InputStream in = context.getResourceAsStream(path);
        assert (in != null);
        Properties p = new Properties();
        p.load(in);
        in.close();
        return p;
    }
*/

    // ---------------------------------------------- support class

    /**
     * ResourceBundle Adapter class.
     */
    public static class ResourceBundleAdapter extends Properties {
        public ResourceBundleAdapter(ResourceBundle rb) {
            assert (rb instanceof java.util.PropertyResourceBundle);
            this.rb = rb;
            java.util.Enumeration e = rb.getKeys();
            while (e.hasMoreElements()) {
                Object o = e.nextElement();
                this.put(o, rb.getObject((String) o));
            }
        }

        private ResourceBundle rb = null;

        public ResourceBundle getBundle(String baseName) {
            return ResourceBundle.getBundle(baseName);
        }

        public ResourceBundle getBundle(String baseName, Locale locale) {
            return ResourceBundle.getBundle(baseName, locale);
        }

        public ResourceBundle getBundle(String baseName,
Locale locale, ClassLoader loader) {
            return ResourceBundle.getBundle(baseName, locale, loader);
        }

        public Enumeration<String> getKeys() {
            return rb.getKeys();
        }

        public Locale getLocale() {
            return rb.getLocale();
        }

        public Object getObject(String key) {
            return rb.getObject(key);
        }

        public String getString(String key) {
            return rb.getString(key);
        }

        public String[] getStringArray(String key) {
            return rb.getStringArray(key);
        }

        protected Object handleGetObject(String key) {
            return ((PropertyResourceBundle) rb).handleGetObject(key);
        }

    }

}
JPropertiesTest.java文件


package com.kindani.test;

import junit.framework.*;
import com.kindani.JProperties;

//import javax.servlet.ServletContext;
import java.util.Properties;

public class JPropertiesTest extends TestCase {
    JProperties jProperties;
    String key = "helloworld.title";
    String value = "Hello World!";

    public void testLoadProperties() throws Exception {
        String name = null;
        Properties p = new Properties();

        name = "C:\\IDEAP\\Properties4Methods\\src\\com\\
kindani\\test\\LocalStrings.properties";
        p = JProperties.loadProperties(name, JProperties.BY_PROPERTIES);
        assertEquals(value, p.getProperty(key));

        name = "com.kindani.test.LocalStrings";
        p = JProperties.loadProperties(name,JProperties.BY_RESOURCEBUNDLE);
        assertEquals(value, p.getProperty(key));
        assertEquals(value,((JProperties.ResourceBundleAdapter)p).getString(key));

        name = "C:\\IDEAP\\Properties4Methods\\src\\com\\
kindani\\test\\LocalStrings.properties";
        p = JProperties.loadProperties(name, JProperties.BY_PROPERTYRESOURCEBUNDLE);
        assertEquals(value, p.getProperty(key));
        assertEquals(value,((JProperties.ResourceBundleAdapter)p).getString(key));

        name = "\\com\\kindani\\test\\LocalStrings.properties";
        p = JProperties.loadProperties(name, JProperties.BY_SYSTEM_CLASSLOADER);
        assertEquals(value, p.getProperty(key));

        name = "\\com\\kindani\\test\\LocalStrings.properties";
        p = JProperties.loadProperties(name, JProperties.BY_CLASSLOADER);
        assertEquals(value, p.getProperty(key));

        name = "test\\LocalStrings.properties";
        p = JProperties.loadProperties(name, JProperties.BY_CLASS);
        assertEquals(value, p.getProperty(key));
    }

/*
    public void testLoadProperties2() throws Exception {
        ServletContext context = null;
        String path = null;
        Properties p = null;
        path = "/WEB-INF/classes/LocalStrings.properties";
        p = JProperties.loadProperties(context, path);
        assertEquals(value, p.getProperty(key));
    }
*/
}
properties文件与JPropertiesTest.java文件相同的目录下

LocalStrings.properties文件


# $Id: LocalStrings.properties,v 1.1 2000/08/17 00:57:52 horwat Exp $

# Default localized resources for example servlets
# This locale is en_US

helloworld.title=Hello World!

requestinfo.title=Request Information Example
requestinfo.label.method=Method:
requestinfo.label.requesturi=Request URI:
requestinfo.label.protocol=Protocol:
requestinfo.label.pathinfo=Path Info:
requestinfo.label.remoteaddr=Remote Address:

requestheader.title=Request Header Example

requestparams.title=Request Parameters Example
requestparams.params-in-req=Parameters in this request:
requestparams.no-params=No Parameters, Please enter some
requestparams.firstname=First Name:
requestparams.lastname=Last Name:

cookies.title=Cookies Example
cookies.cookies=Your browser is sending the following cookies:
cookies.no-cookies=Your browser isn't sending any cookies
cookies.make-cookie=Create a cookie to send to your browser
cookies.name=Name:
cookies.value=Value:
cookies.set=You just sent the following cookie to your browser:

sessions.title=Sessions Example
sessions.id=Session ID:
sessions.created=Created:
sessions.lastaccessed=Last Accessed:
sessions.data=The following data is in your session:
sessions.adddata=Add data to your session
sessions.dataname=Name of Session Attribute:
sessions.datavalue=Value of Session Attribute:
分享到:
评论

相关推荐

    使用J2SE API读取Properties文件的六种方法

    本文将详细讲解使用J2SE API来读取Properties文件的六种方法。 1. **使用java.util.Properties类的load()方法** 这是最基本的方法,通过`FileInputStream`打开文件,然后使用`Properties`类的`load()`方法加载内容...

    J2SE API读取Properties文件六种方法

    以上介绍了六种使用J2SE API读取Properties文件的方法,每种方法都有其适用场景。开发者可以根据实际需求选择最适合的方式来加载配置文件。需要注意的是,在处理输入流时,一定要确保及时关闭它们,避免资源泄露的...

    使用J2SEAPI读取Properties文件的六种方法

    本文将详细介绍六种使用J2SE API(Java标准版API)来读取Properties文件的方法,帮助开发者更好地理解和操作这类文件。 1. 使用Properties类的load()方法 这是最基础且常用的方法,直接通过InputStream读取...

    API读取Properties文件的六种方法

    本文将详细介绍六种使用J2SE API读取`Properties`文件的方法。 #### 1. 使用 `java.util.Properties` 类的 `load()` 方法 这种方法是最常见的读取`Properties`文件的方式。它涉及到创建一个`InputStream`来读取...

    J2SE_API J2SE_API

    J2SE_API、J2SE_API、J2SE_API、J2SE_API、J2SE_API、J2SE_API、J2SE_API

    j2se api 中文版

    J2SE API 中文版是这个平台的标准版API的中文文档,旨在帮助中国开发者更好地理解和使用Java语言。这个压缩包包含了对J2SE API的详细说明,使得开发者在进行Java编程时能够快速查找和理解各种类、接口、方法和异常等...

    J2SE API中文版

    J2SE API中文版是Java开发者的重要参考资料,它包含了Java语言的核心库,包括基础类库、集合框架、I/O流、网络编程、多线程、反射等关键模块的详细文档,为开发者提供清晰的接口说明和方法解释。 1. **基础类库**:...

    j2se中文api.rar

    API(Application Programming Interface)文档详细介绍了 Java 语言的各种类库、接口、方法等,帮助开发者理解如何使用这些组件进行编程。 J2SE API 包含了大量的核心类库,如集合框架、输入/输出流、网络编程、多...

    j2SE中文API帮助文档

    J2SE中文API帮助文档是Java开发者的重要参考资料,它提供了详细的类和方法说明,帮助程序员理解和使用Java语言进行开发。 在J2SE API中文版中,你可以找到以下关键知识点: 1. **类库概述**:J2SE包含了一系列核心...

    J2SE中文API速查手册.chm

    J2SE基本上是Java开发人员必学的基础技能之一,对于J2SE需要经常查询提供类库的使用方法。这里提供了中文版本的J2SE API速查手册,方便开发人员的查询和学习

    J2SE API CHM格式,文件2

    J2SE API CHM格式,第二个压缩文件,由于50兆上传限制

    j2se api 5.0 中文版

    真正的j2se api 5.0 中文版帮助,有需要的可以下载.

    J2SE6.0API+JAVA-API1.6中文版合集

    Java开发人员在编程过程中,经常会参考API文档来了解类库的功能和使用方法。"J2SE6.0 API + JAVA-API1.6中文版合集"是一个非常实用的资源,它整合了Java Standard Edition(J2SE)6.0版本和Java API 1.6的中文文档,...

    J2SE 和 J2EE api 帮助文档

    Java平台由多个组件构成,其中J2SE(Java 2 Platform, Standard Edition)和J2EE(Java 2 Platform, Enterprise Edition)是两个关键部分,它们都提供了丰富的API供开发者使用。本文将深入探讨这两个平台及其API,...

    J2SE6.0 API 中文版(冷冬大雪)

    使用这个中文版的J2SE 6.0 API,开发者可以更方便地查找和理解各种类、接口和方法,从而更高效地进行开发工作。免费提供的特性使得更多的人能够接触到Java技术,推动了Java社区的发展。 总的来说,J2SE 6.0 API中文...

    JAVA_API.rar_JAVA_API_j2se j2EE api_j2se api j2ee api_j2se j2ee

    开发者可以使用它快速查找类、接口、方法和构造函数的详细信息,理解它们的功能和用法,从而提高开发效率。 这些API文档对于Java开发者至关重要,无论你是初学者还是经验丰富的程序员,都能从中受益。通过深入学习...

    J2SE API应用程序接口使用文档

    Java 2 Standard Edition (J2SE) API 应用程序接口使用文档是Java开发者的重要参考资料,它包含了Java语言的核心库,这些库提供了大量的类和接口,用于构建各种类型的桌面应用程序、服务器端应用以及网络服务。...

    J2SE 中文API

    官方的中文J2SE api文档,java开发人员必备。

Global site tag (gtag.js) - Google Analytics