使用Java读取Properties资源文件的6种方法

阅读: 评论:0

2024年1月26日发(作者:)

使用Java读取Properties资源文件的6种方法

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

1.使用ties类的load()方法

示例:

InputStream in = lnew BufferedInputStream(new FileInputStream(name));

Properties p = new Properties();

(in);

2.使用ceBundle类的getBundle()方法

示例: ResourceBundle rb = dle(name, ault());

3.使用tyResourceBundle类的构造函数

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

ResourceBundle rb = new PropertyResourceBundle(in);

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

示例: InputStream in = ourceAsStream(name);

Properties p = new Properties();

(in);

5.使用ssLoader()所得到的oader的getResourceAsStream()方法

示例: InputStream in =

ssLoader().getResourceAsStream(name);

Properties p = new Properties();

(in);

6.使用oader类的getSystemResourceAsStream()静态方法

示例: InputStream in = temResourceAsStream(name);

Properties p = new Properties();

(in);

补充

Servlet中可以使用tContext的getResourceAsStream()方法

示例:InputStream in = ourceAsStream(path);

Properties p = new Properties();

(in);

完整的示例:

文件

package i;

//import tContext;

import .*;

import tream;

import ption;

import edInputStream;

import putStream;

/**

* 使用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);

(in);

} else if (type == BY_RESOURCEBUNDLE) {

ResourceBundle rb = dle(name, ault());

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 ((new JProperties().getClass()));

in = ourceAsStream(name);

assert (in != null);

(in);

//return new JProperties().getClass().getResourceAsStream(name);

} else if (type == BY_CLASSLOADER) {

assert (ssLoader().equals(

new JProperties().getClass().getClassLoader()));

in = ssLoader().getResourceAsStream(name);

assert (in != null);

(in);

//return new JProperties().getClass().

getClassLoader().getResourceAsStream(name);

} else if (type == BY_SYSTEM_CLASSLOADER) {

in = temResourceAsStream(name);

assert (in != null);

(in);

}

if (in != null) {

();

}

return p;

}

// ---------------------------------------------- servlet used

/*

public static Properties loadProperties(

ServletContext context, String path) throws IOException {

assert (context != null);

InputStream in = ourceAsStream(path);

assert (in != null);

Properties p = new Properties();

(in);

();

return p;

}

*/

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

/**

* ResourceBundle Adapter class.

*/

public static class ResourceBundleAdapter extends Properties {

public ResourceBundleAdapter(ResourceBundle rb) {

assert (rb instanceof tyResourceBundle);

= rb;

ation e = s();

while (eElements()) {

Object o = ement();

(o, ect((String) o));

}

}

private ResourceBundle rb = null;

public ResourceBundle getBundle(String baseName) {

return dle(baseName);

}

public ResourceBundle getBundle(String baseName, Locale locale) {

return dle(baseName, locale);

}

public ResourceBundle getBundle(String baseName,

Locale locale, ClassLoader loader) {

return dle(baseName, locale, loader);

}

public Enumeration getKeys() {

return s();

}

public Locale getLocale() {

return ale();

}

public Object getObject(String key) {

return ect(key);

}

public String getString(String key) {

return ing(key);

}

public String[] getStringArray(String key) {

return ingArray(key);

}

protected Object handleGetObject(String key) {

return ((PropertyResourceBundle) rb).handleGetObject(key);

}

}

}

文件package ;

import ork.*;

import rties;

//import tContext;

import ties;

public class JPropertiesTest extends TestCase {

JProperties jProperties;

String key = "";

String value = "Hello World!";

public void testLoadProperties() throws Exception {

String name = null;

Properties p = new Properties();

name = "C:IDEAPProperties4Methodssrccom

ties";

p = operties(name, _PROPERTIES);

assertEquals(value, perty(key));

name = "trings";

p = operties(name,_RESOURCEBUNDLE);

assertEquals(value, perty(key));

assertEquals(value,((ceBundleAdapter)p).getString(key));

name = "C:IDEAPProperties4Methodssrccom

ties";

p = operties(name,

_PROPERTYRESOURCEBUNDLE);

assertEquals(value, perty(key));

assertEquals(value,((ceBundleAdapter)p).getString(key));

name = "ties";

p = operties(name, _SYSTEM_CLASSLOADER);

assertEquals(value, perty(key));

name = "ties";

p = operties(name, _CLASSLOADER);

assertEquals(value, perty(key));

name = "ties";

p = operties(name, _CLASS);

assertEquals(value, perty(key));

}

/*

public void testLoadProperties2() throws Exception {

ServletContext context = null;

String path = null;

Properties p = null;

path = "/Web-INF/classes/ties";

p = operties(context, path);

assertEquals(value, perty(key));

}

*/

}

properties文件与文件相同的目录下

ties文件

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

# Default localized resources for example Servlets

# This locale is en_US

=Hello World!

=Request Information Example

=Method:

turi=Request URI:

ol=Protocol:

fo=Path Info:

addr=Remote Address:

=Request Header Example

=Request Parameters Example

-in-req=Parameters in this request:

-params=No Parameters, Please enter some

ame=First Name:

me=Last Name:

=Cookies Example

s=Your browser is sending the following cookies:

-cookies=Your browser isn't sending any cookies

-cookie=Create a cookie to send to your browser

=Name:

=Value:

=You just sent the following cookie to your browser:

=Sessions Example

=Session ID:

d=Created:

cessed=Last Accessed:

=The following data is in your session:

a=Add data to your session

me=Name of Session Attribute:

lue=Value of Session Attribute

使用Java读取Properties资源文件的6种方法

本文发布于:2024-01-26 03:39:33,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/1706211573671.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:文件   读取   使用
留言与评论(共有 0 条评论)
   
验证码:
排行榜

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23