当前位置: 移动技术网 > IT编程>开发语言>Java > JSP 中Spring的Resource类读写中文Properties实例代码

JSP 中Spring的Resource类读写中文Properties实例代码

2017年12月08日  | 移动技术网IT编程  | 我要评论

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


感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网