服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

用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 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 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.test; 

import junit.framework.*; 
import com.kindani.jproperties; 

//import javax.servlet.servletcontext; 
import java.util.properties; 

public class jpropertiestest extends testcase { 
jproperties jproperties; 
string key = "helloworld.title"; 
string value = "hello world!"; 

public void testloadproperties() throws exception { 
string name = null; 
properties p = new properties(); 

name = "c:\\ideap\\properties4methods\\src\\com\\kindani\\test\\localstrings.properties"; 
p = jproperties.loadproperties(name, jproperties.by_properties); 
assertequals(value, p.getproperty(key)); 

name = "com.kindani.test.localstrings"; 
p = jproperties.loadproperties(name,jproperties.by_resourcebundle); 
assertequals(value, p.getproperty(key)); 
assertequals(value,((jproperties.resourcebundleadapter)p).getstring(key)); 

name = "c:\\ideap\\properties4methods\\src\\com\\kindani\\test\\localstrings.properties"; 
p = jproperties.loadproperties(name, jproperties.by_propertyresourcebundle); 
assertequals(value, p.getproperty(key)); 
assertequals(value,((jproperties.resourcebundleadapter)p).getstring(key)); 

name = "\\com\\kindani\\test\\localstrings.properties"; 
p = jproperties.loadproperties(name, jproperties.by_system_classloader); 
assertequals(value, p.getproperty(key)); 

name = "\\com\\kindani\\test\\localstrings.properties"; 
p = jproperties.loadproperties(name, jproperties.by_classloader); 
assertequals(value, p.getproperty(key)); 

name = "test\\localstrings.properties"; 
p = jproperties.loadproperties(name, jproperties.by_class); 
assertequals(value, p.getproperty(key)); 


/* 
public void testloadproperties2() throws exception { 
servletcontext context = null; 
string path = null; 
properties p = null; 
path = "/web-inf/classes/localstrings.properties"; 
p = jproperties.loadproperties(context, path); 
assertequals(value, p.getproperty(key)); 

*/ 


properties文件与jpropertiestest.java文件相同的目录下 
localstrings.properties文件 
# $id: localstrings.properties,v 1.1 2000/08/17 00:57:52 horwat exp $ 

# default localized resources for example servlets 
# this locale is en_us 

helloworld.title=hello world! 

requestinfo.title=request information example 
requestinfo.label.method=method: 
requestinfo.label.requesturi=request uri: 
requestinfo.label.protocol=protocol: 
requestinfo.label.pathinfo=path info: 
requestinfo.label.remoteaddr=remote address: 

requestheader.title=request header example 

requestparams.title=request parameters example 
requestparams.params-in-req=parameters in this request: 
requestparams.no-params=no parameters, please enter some 
requestparams.firstname=first name: 
requestparams.lastname=last name: 

cookies.title=cookies example 
cookies.cookies=your browser is sending the following cookies: 
cookies.no-cookies=your browser isn't sending any cookies 
cookies.make-cookie=create a cookie to send to your browser 
cookies.name=name: 
cookies.value=value: 
cookies.set=you just sent the following cookie to your browser: 

sessions.title=sessions example 
sessions.id=session id: 
sessions.created=created: 
sessions.lastaccessed=last accessed: 
sessions.data=the following data is in your session: 
sessions.adddata=add data to your session 
sessions.dataname=name of session attribute: 
sessions.datavalue=value of session attribute: