jsp 中spring的resource类读写中文properties
摘要: spring对properties的读取进行了完善而全面的封装,对于写则仍需配合fileoutputstream进行。
package com.oolong.common.util;
import org.springframework.core.io.support.pathmatchingresourcepatternresolver;
import org.springframework.core.io.support.resourcepatternresolver;
import java.io.*;
import java.util.*;
public class uservar {
private static string configfile = "classpath*:param.properties";
private static org.springframework.core.io.resource resourcewritable;
private static properties p;
/**
* 注意事项:
* 1、properties放在source目录下
* 2、param.properties至少有一对键值对
*/
static {
p = new properties();
org.springframework.core.io.resource[] resources = null;
try {
resourcepatternresolver resolver = new pathmatchingresourcepatternresolver();
resources = resolver.getresources(configfile);
if (resources != null) {
for (org.springframework.core.io.resource r : resources) {
if (r != null) {
p.load(r.getinputstream());
resourcewritable = r;
}
}
}
} catch (ioexception e1) {
e1.printstacktrace();
}
}
public static string get(string key) {
string v = (string) p.get(key);
if (v != null) {
try {
return new string(v.getbytes("iso-8859-1"), "gbk");
} catch (unsupportedencodingexception e) {
e.printstacktrace();
return null;
}
}
return null;
}
public static void set(string key,string value){
if (null != resourcewritable) {
try {
outputstream fos = new fileoutputstream(resourcewritable.getfile());
properties p = new properties();
p.load(resourcewritable.getinputstream());
value = new string(value.getbytes("gbk"),"iso-8859-1");
p.setproperty(key, value);
p.store(fos, null);
fos.close();
} catch (ioexception e) {
e.printstacktrace();
}
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!