2024年3月31日发(作者:)

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