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

来源:互联网 发布:python six.iteritems 编辑:程序博客网 时间:2024/05/18 16:19

使用J2SE API读取Properties文件的六种方法
1。使用java.util.Properties类的load()方法
示例:
InputStream in = lnew BufferedInputStream(new FileInputStream(name));

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

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

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

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

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

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

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

完整的示例,可以参考附件文件
如何上传文件,谁知道请告诉以下。 只好把source都贴上来了

JProperties.java文件
** 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.

** the file LICENSE, as well as License.html. If the license is not

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

** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.

** THIS SOFTWARE IS PROVIDED AS-IS 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 com.kindani;
//import javax.servlet.ServletContext;
import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
* 使用J2SE API?取Properties文件的六?方法
* User: SYNFORM
* 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 {

InputStream in = null;
if (type == BY_PROPERTIES) {
in = new BufferedInputStream(new FileInputStream(name));

} else if (type == BY_RESOURCEBUNDLE) {
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

p = new ResourceBundleAdapter(rb);
} else if (type == BY_PROPERTYRESOURCEBUNDLE) {
in = new BufferedInputStream(new FileInputStream(name));

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

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

} else if (type == BY_SYSTEM_CLASSLOADER) {
in = ClassLoader.getSystemResourceAsStream(name);
public static Properties loadProperties(ServletContext context, String path) throws IOException {

原创粉丝点击