使用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);
完整的示例,可以参考附件文件
如何上传文件,谁知道请告诉以下。 只好把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.
** version 2 of the license should be included with this distribution in
** 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
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** 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.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文件
/**
** 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 2 of the license should be included with this distribution in
** 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
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** free software foundation, 675 mass ave, cambridge, ma 02139 usa.
**
** this
闽公网安备 35060202000074号