欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JSP 中Spring的Resource類讀寫中文Properties實(shí)例代碼

 更新時(shí)間:2017年03月29日 11:20:11   投稿:lqh  
這篇文章主要介紹了JSP 中Spring的Resource類讀寫中文Properties實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下

JSP 中Spring的Resource類讀寫中文Properties

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

相關(guān)文章

最新評(píng)論