JSP 中Spring的Resource类读写中文Properties实例代码
JSP中Spring的Resource类读写中文Properties
摘要:Spring对Properties的读取进行了完善而全面的封装,对于写则仍需配合FileOutputStream进行。
packagecom.oolong.common.util; importorg.springframework.core.io.support.PathMatchingResourcePatternResolver; importorg.springframework.core.io.support.ResourcePatternResolver; importjava.io.*; importjava.util.*; publicclassUserVar{ privatestaticStringconfigFile="classpath*:param.properties"; privatestaticorg.springframework.core.io.ResourceresourceWritable; privatestaticPropertiesp; /** *注意事项: *1、properties放在source目录下 *2、param.properties至少有一对键值对 */ static{ p=newProperties(); org.springframework.core.io.Resource[]resources=null; try{ ResourcePatternResolverresolver=newPathMatchingResourcePatternResolver(); resources=resolver.getResources(configFile); if(resources!=null){ for(org.springframework.core.io.Resourcer:resources){ if(r!=null){ p.load(r.getInputStream()); resourceWritable=r; } } } }catch(IOExceptione1){ e1.printStackTrace(); } } publicstaticStringget(Stringkey){ Stringv=(String)p.get(key); if(v!=null){ try{ returnnewString(v.getBytes("ISO-8859-1"),"GBK"); }catch(UnsupportedEncodingExceptione){ e.printStackTrace(); returnnull; } } returnnull; } publicstaticvoidset(Stringkey,Stringvalue){ if(null!=resourceWritable){ try{ OutputStreamfos=newFileOutputStream(resourceWritable.getFile()); Propertiesp=newProperties(); p.load(resourceWritable.getInputStream()); value=newString(value.getBytes("GBK"),"ISO-8859-1"); p.setProperty(key,value); p.store(fos,null); fos.close(); }catch(IOExceptione){ e.printStackTrace(); } } } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!