濮阳杆衣贸易有限公司

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

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

熱門標(biāo)簽:好操作的電話機器人廠家 如何用中國地圖標(biāo)注數(shù)字點 地圖標(biāo)注市場怎么樣 百度地圖添加標(biāo)注圖標(biāo)樣式 企業(yè)怎么在聯(lián)通申請400電話 南昌市地圖標(biāo)注app 南京新思維電話機器人 泰州泰興400電話 怎么申請 聊城智能電銷機器人外呼

JSP 中Spring的Resource類讀寫中文Properties

摘要: Spring對Properties的讀取進(jìn)行了完善而全面的封裝,對于寫則仍需配合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;

 /**
  * 注意事項:
  * 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加載資源用法示例

標(biāo)簽:銅川 烏蘭察布 臨汾 白銀 開封 山南 吉林 自貢

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP 中Spring的Resource類讀寫中文Properties實例代碼》,本文關(guān)鍵詞  JSP,中,Spring,的,Resource,類,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《JSP 中Spring的Resource類讀寫中文Properties實例代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于JSP 中Spring的Resource類讀寫中文Properties實例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    阿克| 大足县| 永福县| 交城县| 鄂温| 荥经县| 大新县| 建始县| 肥东县| 买车| 通海县| 红安县| 明水县| 江安县| 上虞市| 达州市| 孙吴县| 阳江市| 尼玛县| 施甸县| 广元市| 达州市| 邛崃市| 沐川县| 商城县| 佳木斯市| 鲁山县| 平度市| 黄浦区| 名山县| 桂林市| 隆尧县| 公安县| 静海县| 喜德县| 平陆县| 苍溪县| 侯马市| 建阳市| 宝清县| 宜兴市|