电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

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


发布日期:2018/4/22
 

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

使用javautilProperties类的load()方法

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

Properties p = new Properties();

pload(in);

使用javautilResourceBundle类的getBundle()方法

示例 ResourceBundle rb = ResourceBundlegetBundle(name LocalegetDefault());

使用javautilPropertyResourceBundle类的构造函数

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

ResourceBundle rb = new PropertyResourceBundle(in);

使用class变量的getResourceAsStream()方法

示例 InputStream in = JPropertiesclassgetResourceAsStream(name);

Properties p = new Properties();

pload(in);

使用classgetClassLoader()所得到的javalangClassLoader的getResourceAsStream()方法

示例 InputStream in = JPropertiesclassgetClassLoader()getResourceAsStream(name);

Properties p = new Properties();

pload(in);

使用javalangClassLoader类的getSystemResourceAsStream()静态方法

示例 InputStream in = ClassLoadergetSystemResourceAsStream(name);

Properties p = new Properties();

pload(in);

补充

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

示例InputStream in = contextgetResourceAsStream(path);

Properties p = new Properties();

pload(in);

完整的示例可以参考附件文件

如何上传文件谁知道请告诉以下 只好把source都贴上来了

JPropertiesjava文件

/**

** This program is free software

**

** You may redistribute it and/or modify it under the terms of the GNU

** General Public License as published by the Free Software Foundation

** Version of the license should be included with this distribution in

** the file LICENSE as well as l If the license is not

** included with this distribution you may find a copy at the FSF web

** site at or or you may write to the

** Free Software Foundation Mass Ave Cambridge MA USA

**

** THIS SOFTWARE IS PROVIDED ASIS WITHOUT WARRANTY OF ANY KIND

** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY THE AUTHOR

** OF THIS SOFTWARE ASSUMES _NO_ RESPONSIBILITY FOR ANY

** CONSEQUENCE RESULTING FROM THE USE MODIFICATION OR

** REDISTRIBUTION OF THIS SOFTWARE

**/

package comkindani;

//import javaxservletServletContext;

import javautil*;

import javaioInputStream;

import javaioIOException;

import javaioBufferedInputStream;

import javaioFileInputStream;

/**

* 使用JSE API読取Properties文件的六种方法

* User: SYNFORM

* Date: //

* Time: ::

* To change this template use File | Settings | File Templates

*/

public class JProperties {

public final static int BY_PROPERTIES = ;

public final static int BY_RESOURCEBUNDLE = ;

public final static int BY_PROPERTYRESOURCEBUNDLE = ;

public final static int BY_CLASS = ;

public final static int BY_CLASSLOADER = ;

public final static int BY_SYSTEM_CLASSLOADER = ;

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

pload(in);

} else if (type == BY_RESOURCEBUNDLE) {

ResourceBundle rb = ResourceBundlegetBundle(name LocalegetDefault());

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

in = JPropertiesclassgetResourceAsStream(name);

assert (in != null);

pload(in);

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

} else if (type == BY_CLASSLOADER) {

assert (JPropertiesclassgetClassLoader()equals(new JProperties()getClass()getClassLoader()));

in = JPropertiesclassgetClassLoader()getResourceAsStream(name);

assert (in != null);

pload(in);

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

} else if (type == BY_SYSTEM_CLASSLOADER) {

in = ClassLoadergetSystemResourceAsStream(name);

assert (in != null);

pload(in);

}

if (in != null) {

inclose();

}

return p;

}

// servlet used

/*

public static Properties loadProperties(ServletContext context String path) throws IOException {

assert (context != null);

InputStream in = contextgetResourceAsStream(path);

assert (in != null);

Properties p = new Properties();

pload(in);

inclose();

return p;

}

*/

// support class

/**

* ResourceBundle Adapter class

*/

public static class ResourceBundleAdapter extends Properties {

public ResourceBundleAdapter(ResourceBundle rb) {

assert (rb instanceof javautilPropertyResourceBundle);

thisrb = rb;

javautilEnumeration e = rbgetKeys();

while (ehasMoreElements()) {

Object o = enextElement();

thisput(o rbgetObject((String) o));

}

}

private ResourceBundle rb = null;

public ResourceBundle getBundle(String baseName) {

return ResourceBundlegetBundle(baseName);

}

public ResourceBundle getBundle(String baseName Locale locale) {

return ResourceBundlegetBundle(baseName locale);

}

public ResourceBundle getBundle(String baseName Locale locale ClassLoader loader) {

return ResourceBundlegetBundle(baseName locale loader);

}

public Enumeration<String> getKeys() {

return rbgetKeys();

}

public Locale getLocale() {

return rbgetLocale();

}

public Object getObject(String key) {

return rbgetObject(key);

}

public String getString(String key) {

return rbgetString(key);

}

public String[] getStringArray(String key) {

return rbgetStringArray(key);

}

protected Object handleGetObject(String key) {

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

}

}

}

JPropertiesTestjava文件

/**

** This program is free software

**

** You may redistribute it and/or modify it under the terms of the GNU

** General Public License as published by the Free Software Foundation

** Version of the license should be included with this distribution in

** the file LICENSE as well as l If the license is not

** included with this distribution you may find a copy at the FSF web

** site at or or you may write to the

** Free Software Foundation Mass Ave Cambridge MA USA

**

** THIS

上一篇:抽象的进步

下一篇:通用上载组件的原理及实现