j2se读取Properties文件的6种方法Word下载.docx
《j2se读取Properties文件的6种方法Word下载.docx》由会员分享,可在线阅读,更多相关《j2se读取Properties文件的6种方法Word下载.docx(7页珍藏版)》请在冰豆网上搜索。
InputStreamin=JProperties.class.getResourceAsStream(name);
5。
使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
InputStreamin=JProperties.class.getClassLoader().getResourceAsStream(name);
6。
使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
InputStreamin=ClassLoader.getSystemResourceAsStream(name);
补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
InputStreamin=context.getResourceAsStream(path);
完整的示例,可以参考附件文件
如何上传文件,谁知道请告诉以下。
只好把source都贴上来了
JProperties.java文件
/**
**Thisprogramisfreesoftware.
**
**Youmayredistributeitand/ormodifyitunderthetermsoftheGNU
**GeneralPublicLicenseaspublishedbytheFreeSoftwareFoundation.
**Version2ofthelicenseshouldbeincludedwiththisdistributionin
**thefileLICENSE,aswellasLicense.html.Ifthelicenseisnot
**includedwiththisdistribution,youmayfindacopyattheFSFweb
**siteat'
www.gnu.org'
or'
www.fsf.org'
oryoumaywritetothe
**FreeSoftwareFoundation,675MassAve,Cambridge,MA02139USA.
**THISSOFTWAREISPROVIDEDAS-ISWITHOUTWARRANTYOFANYKIND,
**NOTEVENTHEIMPLIEDWARRANTYOFMERCHANTABILITY.THEAUTHOR
**OFTHISSOFTWARE,ASSUMES_NO_RESPONSIBILITYFORANY
**CONSEQUENCERESULTINGFROMTHEUSE,MODIFICATION,OR
**REDISTRIBUTIONOFTHISSOFTWARE.
**/
packagecom.kindani;
//importjavax.servlet.ServletContext;
importjava.util.*;
importjava.io.InputStream;
importjava.io.IOException;
importjava.io.BufferedInputStream;
importjava.io.FileInputStream;
*使用J2SEAPI?
取Properties文件的六?
方法
*User:
SYNFORM
*Date:
2005/07/12
*Time:
18:
40:
55
*TochangethistemplateuseFile|Settings|FileTemplates.
*/
publicclassJProperties{
publicfinalstaticintBY_PROPERTIES=1;
publicfinalstaticintBY_RESOURCEBUNDLE=2;
publicfinalstaticintBY_PROPERTYRESOURCEBUNDLE=3;
publicfinalstaticintBY_CLASS=4;
publicfinalstaticintBY_CLASSLOADER=5;
publicfinalstaticintBY_SYSTEM_CLASSLOADER=6;
publicfinalstaticPropertiesloadProperties(finalStringname,finalinttype)throwsIOException{
InputStreamin=null;
if(type==BY_PROPERTIES){
in=newBufferedInputStream(newFileInputStream(name));
assert(in!
=null);
}elseif(type==BY_RESOURCEBUNDLE){
assert(rb!
p=newResourceBundleAdapter(rb);
}elseif(type==BY_PROPERTYRESOURCEBUNDLE){
}elseif(type==BY_CLASS){
assert(JProperties.class.equals(newJProperties().getClass()));
in=JProperties.class.getResourceAsStream(name);
//returnnewJProperties().getClass().getResourceAsStream(name);
}elseif(type==BY_CLASSLOADER){
assert(JProperties.class.getClassLoader().equals(newJProperties().getClass().getClassLoader()));
in=JProperties.class.getClassLoader().getResourceAsStream(name);
//returnnewJProperties().getClass().getClassLoader().getResourceAsStream(name);
}elseif(type==BY_SYSTEM_CLASSLOADER){
in=ClassLoader.getSystemResourceAsStream(name);
}
if(in!
=null){
in.close();
returnp;
//----------------------------------------------servletused
/*
publicstaticPropertiesloadProperties(ServletContextcontext,Stringpath)throwsIOException{
assert(context!
//----------------------------------------------supportclass
*ResourceBundleAdapterclass.
publicstaticclassResourceBundleAdapterextendsProperties{
publicResourceBundleAdapter(ResourceBundlerb){
assert(rbinstanceofjava.util.PropertyResourceBundle);
this.rb=rb;
java.util.Enumeratione=rb.getKeys();
while(e.hasMoreElements()){
Objecto=e.nextElement();
this.put(o,rb.getObject((String)o));
privateResourceBundlerb=null;
publicResourceBundlegetBundle(StringbaseName){
returnResourceBundle.getBundle(baseName);
publicResourceBundlegetBundle(StringbaseName,Localelocale){
returnResourceBundle.getBundle(baseName,locale);
publicResourceBundlegetBundle(StringbaseName,Localelocale,ClassLoaderloader){
returnResourceBundle.getBundle(baseName,locale,loader);
publicEnumerationgetKeys(){
returnrb.getKeys();
publicLocalegetLocale(){
returnrb.getLocale();
publicObjectgetObject(Stringkey){
returnrb.getObject(key);
publicStringgetString(Stringkey){
returnrb.getString(key);
publicString[]getStringArray(Stringkey){
returnrb.getStringArray(key);
protectedObjecthandleGetObject(Stringkey){
return((PropertyResourceBundle)rb).handleGetObject(key);
JPropertiesTest.java文件
packagecom.kindani.test;
importjunit.framework.*;
importcom.kindani.JProperties;
importjava.util.Properties;
publicclassJPropertiesTestextendsTestCase{
JPropertiesjProperties;
Stringkey="
helloworld.title"
;
Stringvalue="
HelloWorld!
"
publicvoidtestLoadProperties()throwsException{
Stringname=null;
name="
C:
\\IDEAP\\Properties4Methods\\src\\com\\kindani\\test\\LocalStrings.properties"
p=JProperties.loadProperties(name,JProperties.BY_PROPERTIES);
assertEquals(value,p.getProperty(key));
com.kindani.test.LocalStrings"
p=JProperties.loadProperties(name,JProperties.BY_RESOURCEBUNDLE);
assertEquals(value,((JProperties.ResourceBundleAdapter)p).getString(key));
p=JProperties.loadProperties(name,JProperties.BY_PROPERTYRESOURCEBUNDLE);
\\com\\kindani\\test\\LocalStrings.properties"
p=JProperties.loadProperties(name,JProperties.BY_SYSTEM_CLASSLOADER);
p=JProperties.loadProperties(name,JProperties.BY_CLASSLOADER);
test\\LocalStrings.properties"
p=JProperties.loadProperties(name,JProperties.BY_CLASS);
publicvoidtestLoadProperties2()throwsException{
ServletContextcontext=null;
Stringpath=null;
Propertiesp=null;
path="
/WEB-INF/classes/LocalStrings.properties"
p=JProperties.loadProperties(context,path);
properties文件与JPropertiesTest.java文件相同的目录下
LocalStrings.properties文件
#$Id:
LocalStrings.properties,v1.12000/08/1700:
57:
52horwatExp$
#Defaultlocalizedresourcesforexampleservlets
#Thislocaleisen_US
helloworld.title=HelloWorld!
requestinfo.title=RequestInformationExample
requestinfo.label.method=Method:
requestinfo.label.requesturi=RequestURI:
requestinfo.label.protocol=Protocol:
requestinfo.label.pathinfo=PathInfo:
requestinfo.label.remoteaddr=RemoteAddress:
requestheader.title=RequestHeaderExample
requestparams.title=RequestParametersExample
requestparams.params-in-req=Parametersinthisrequest:
requestparams.no-params=NoParameters,Pleaseentersome
requestparams.firstname=FirstName:
requestparams.lastname=LastName:
cookies.title=CookiesExample
cookies.cookies=Yourbrowserissendingthefollowingcookies:
cookies.no-cookies=Yourbrowserisn'
tsendinganycookies
cookies.make-cookie=Createacookietosendtoyourbrowser
cookies.na