JSP 中Spring的Resource類讀寫(xiě)中文Properties實(shí)例代碼
更新時(shí)間:2017年03月29日 11:20:11 投稿:lqh
這篇文章主要介紹了JSP 中Spring的Resource類讀寫(xiě)中文Properties實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
JSP 中Spring的Resource類讀寫(xiě)中文Properties
摘要: Spring對(duì)Properties的讀取進(jìn)行了完善而全面的封裝,對(duì)于寫(xiě)則仍需配合FileOutputStream進(jìn)行。
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;
/**
* 注意事項(xiàng):
* 1、properties放在source目錄下
* 2、param.properties至少有一對(duì)鍵值對(duì)
*/
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();
}
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
您可能感興趣的文章:
- Spring實(shí)戰(zhàn)之ResourceLoader接口資源加載用法示例
- Spring實(shí)戰(zhàn)之ServletContextResource訪問(wèn)資源文件示例
- Spring實(shí)戰(zhàn)之FileSystemResource加載資源文件示例
- Spring實(shí)戰(zhàn)之使用ClassPathResource加載xml資源示例
- Spring實(shí)戰(zhàn)之使用@Resource配置依賴操作示例
- Spring注解@Resource和@Autowired區(qū)別對(duì)比詳解
- 詳解Spring關(guān)于@Resource注入為null解決辦法
- 詳解SpringBoot開(kāi)發(fā)使用@ImportResource注解影響攔截器
- 詳解Spring注解--@Autowired、@Resource和@Service
- Spring 中 @Service 和 @Resource 注解的區(qū)別
- Spring框架中 @Autowired 和 @Resource 注解的區(qū)別
- Spring實(shí)戰(zhàn)之ResourceLoaderAware加載資源用法示例
相關(guān)文章
實(shí)例講解JSP Model2體系結(jié)構(gòu)(上)
實(shí)例講解JSP Model2體系結(jié)構(gòu)(上)...2006-10-10
網(wǎng)站圖片路徑的問(wèn)題:絕對(duì)路徑/虛擬路徑
圖片和工程不在一個(gè)盤(pán)符下。圖片也不能放到工程下,下面與大家分享下詳細(xì)的解決方法2013-05-05

