Java读取配置文件做法.docx

上传人:b****5 文档编号:7183313 上传时间:2023-01-21 格式:DOCX 页数:10 大小:17.79KB
下载 相关 举报
Java读取配置文件做法.docx_第1页
第1页 / 共10页
Java读取配置文件做法.docx_第2页
第2页 / 共10页
Java读取配置文件做法.docx_第3页
第3页 / 共10页
Java读取配置文件做法.docx_第4页
第4页 / 共10页
Java读取配置文件做法.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

Java读取配置文件做法.docx

《Java读取配置文件做法.docx》由会员分享,可在线阅读,更多相关《Java读取配置文件做法.docx(10页珍藏版)》请在冰豆网上搜索。

Java读取配置文件做法.docx

Java读取配置文件做法

Java读取Properties文件的六种方法

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

1。

使用java.util.Properties类的load()方法

示例:

InputStreamin=newBufferedInputStream(newFileInputStream(name));

Propertiesp=newProperties();

p.load(in);

2。

使用java.util.ResourceBundle类的getBundle()方法

示例:

ResourceBundlerb=ResourceBundle.getBundle(name,Locale.getDefault());

3。

使用java.util.PropertyResourceBundle类的构造函数

示例:

InputStreamin=newBufferedInputStream(newFileInputStream(name));

ResourceBundlerb=newPropertyResourceBundle(in);

4。

使用class变量的getResourceAsStream()方法

示例:

InputStreamin=JProperties.class.getResourceAsStream(name);

Propertiesp=newProperties();

p.load(in);

5。

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

示例:

InputStreamin=JProperties.class.getClassLoader().getResourceAsStream(name);

Propertiesp=newProperties();

p.load(in);

6。

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

示例:

InputStreamin=ClassLoader.getSystemResourceAsStream(name);

Propertiesp=newProperties();

p.load(in);

补充

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

示例:

InputStreamin=context.getResourceAsStream(path);

Propertiesp=newProperties();

p.load(in);

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

如何上传文件,谁知道请告诉以下。

只好把source都贴上来了

JProperties.java文件

packagecom.kindani;

//importjavax.servlet.ServletContext;

importjava.util.*;

importjava.io.InputStream;

importjava.io.IOException;

importjava.io.BufferedInputStream;

importjava.io.FileInputStream;

publicclassJProperties{

publicfinalstaticintBY_PROPERTIES=1;

publicfinalstaticintBY_RESOURCEBUNDLE=2;

publicfinalstaticintBY_PROPERTYRESOURCEBUNDLE=3;

publicfinalstaticintBY_CLASS=4;

publicfinalstaticintBY_CLASSLOADER=5;

publicfinalstaticintBY_SYSTEM_CLASSLOADER=6;

publicfinalstaticPropertiesloadProperties(finalStringname,finalinttype)throwsIOException{

Propertiesp=newProperties();

InputStreamin=null;

if(type==BY_PROPERTIES){

in=newBufferedInputStream(newFileInputStream(name));

assert(in!

=null);

p.load(in);

}elseif(type==BY_RESOURCEBUNDLE){

ResourceBundlerb=ResourceBundle.getBundle(name,Locale.getDefault());

assert(rb!

=null);

p=newResourceBundleAdapter(rb);

}elseif(type==BY_PROPERTYRESOURCEBUNDLE){

in=newBufferedInputStream(newFileInputStream(name));

assert(in!

=null);

ResourceBundlerb=newPropertyResourceBundle(in);

p=newResourceBundleAdapter(rb);

}elseif(type==BY_CLASS){

assert(JProperties.class.equals(newJProperties().getClass()));

in=JProperties.class.getResourceAsStream(name);

assert(in!

=null);

p.load(in);

//returnnewJProperties().getClass().getResourceAsStream(name);

}elseif(type==BY_CLASSLOADER){

assert(JProperties.class.getClassLoader().equals(newJProperties().getClass().getClassLoader()));

in=JProperties.class.getClassLoader().getResourceAsStream(name);

assert(in!

=null);

p.load(in);

//returnnewJProperties().getClass().getClassLoader().getResourceAsStream(name);

}elseif(type==BY_SYSTEM_CLASSLOADER){

in=ClassLoader.getSystemResourceAsStream(name);

assert(in!

=null);

p.load(in);

}

if(in!

=null){

in.close();

}

returnp;

}

//----------------------------------------------servletused

//----------------------------------------------supportclass

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;

//importjavax.servlet.ServletContext;

importjava.util.Properties;

publicclassJPropertiesTestextendsTestCase{

JPropertiesjProperties;

Stringkey="helloworld.title";

Stringvalue="HelloWorld!

";

publicvoidtestLoadProperties()throwsException{

Stringname=null;

Propertiesp=newProperties();

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

}

}

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.name=Name:

cookies.value=Value:

cookies.set=Youjustsentthefollowingcookietoyourbrowser:

sessions.title=SessionsExample

sessions.id=SessionID:

sessions.created=Created:

sessions.lastaccessed=LastAccessed:

sessions.data=Thefollowingdataisinyoursession:

sessions.adddata=Adddatatoyoursession

sessions.dataname=NameofSessionAttribute:

sessions.datavalue=ValueofSessionAttribute:

------------------------------------------------------------------------------------------------------------------------------------------------------------------

Java对properties配置文件的操作

packagecom.yorsun;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.util.Properties;

importjavax.servlet.ServletContext;

importjavax.servlet.http.HttpServlet;

publicclassPropertiesUnit{

privateStringfilename;

privatePropertiesp;

privateFileInputStreamin;

privateFileOutputStreamout;

publicPropertiesUnit(Stringfilename){

this.filename=filename;

Filefile=newFile(filename);

try{

in=newFileInputStream(file);

p=newProperties();

p.load(in);

in.close();

}catch(FileNotFoundExceptione){

//TODOAuto-generatedcatchblock

System.err.println("配置文件config.properties找不到!

");

e.printStackTrace();

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

System.err.println("读取配置文件config.properties错误!

");

e.printStackTrace();

}

}

publicstaticStringgetConfigFile(HttpServleths){

returngetConfigFile(hs,"config.properties");

}

privatestaticStringgetConfigFile(HttpServleths,StringconfigFileName){

StringconfigFile="";

ServletContextsc=hs.getServletContext();

configFile=sc.getRealPath("/"+configFileName);

if(configFile==null||configFile.equals("")){

configFile="/"+configFileName;

}

//TODOAuto-generatedmethodstub

returnconfigFile;

}

publicvoidlist(){

p.list(System.out);

}

publicStringgetValue(StringitemName){

returnp.getProperty(itemName);

}

publicStringgetValue(StringitemName,StringdefaultValue){

returnp.getProperty(itemName,defaultValue);

}

publicvoidsetValue(StringitemName,Stringvalue){

p.setProperty(itemName,value);

}

publicvoidsaveFile(Stringfilename,Stringdescription)throwsException{

try{

Filef=newFile(filename);

out=newFileOutputStream(f)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > 交规考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1