Java读取配置文件做法Word格式文档下载.docx
《Java读取配置文件做法Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Java读取配置文件做法Word格式文档下载.docx(10页珍藏版)》请在冰豆网上搜索。
6。
使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
InputStreamin=ClassLoader.getSystemResourceAsStream(name);
补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
InputStreamin=context.getResourceAsStream(path);
完整的示例,可以参考附件文件
如何上传文件,谁知道请告诉以下。
只好把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{
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
//----------------------------------------------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;
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);
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.FileNotFoundException;
importjava.io.FileOutputStream;
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();
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
System.err.println("
配置文件config.properties找不到!
);
e.printStackTrace();
}catch(IOExceptione){
读取配置文件config.properties错误!
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{
Filef=newFile(filename);
out=newFileOutputStream(f)