佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > JSP 中Spring的Resource類讀寫中文Properties實例代碼

JSP 中Spring的Resource類讀寫中文Properties實例代碼

熱門標簽:團購網(wǎng)站 科大訊飛語音識別系統(tǒng) 阿里云 電子圍欄 銀行業(yè)務 Mysql連接數(shù)設置 Linux服務器 服務器配置

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


感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

您可能感興趣的文章:
  • Spring實戰(zhàn)之ResourceLoader接口資源加載用法示例
  • Spring實戰(zhàn)之ServletContextResource訪問資源文件示例
  • Spring實戰(zhàn)之FileSystemResource加載資源文件示例
  • Spring實戰(zhàn)之使用ClassPathResource加載xml資源示例
  • Spring實戰(zhàn)之使用@Resource配置依賴操作示例
  • Spring注解@Resource和@Autowired區(qū)別對比詳解
  • 詳解Spring關(guān)于@Resource注入為null解決辦法
  • 詳解SpringBoot開發(fā)使用@ImportResource注解影響攔截器
  • 詳解Spring注解--@Autowired、@Resource和@Service
  • Spring 中 @Service 和 @Resource 注解的區(qū)別
  • Spring框架中 @Autowired 和 @Resource 注解的區(qū)別
  • Spring實戰(zhàn)之ResourceLoaderAware加載資源用法示例

標簽:廣元 棗莊 萍鄉(xiāng) 衡水 江蘇 衢州 大理 蚌埠

巨人網(wǎng)絡通訊聲明:本文標題《JSP 中Spring的Resource類讀寫中文Properties實例代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    建瓯市| 祁门县| 南昌县| 灵宝市| 兴城市| 垫江县| 葫芦岛市| 竹溪县| 多伦县| 东乌珠穆沁旗| 桦川县| 高州市| 纳雍县| 额尔古纳市| 汤原县| 北宁市| 长春市| 中超| 永仁县| 鲁甸县| 林州市| 行唐县| 达州市| 波密县| 浏阳市| 乌兰县| 宜昌市| 灌阳县| 大余县| 呼玛县| 元氏县| 临武县| 东辽县| 隆回县| 襄垣县| 攀枝花市| 佛山市| 禄丰县| 白水县| 怀宁县| 临颍县|