首页 > 其他 > 详细

properties配置文件的读取和写入

时间:2015-12-23 19:45:10      阅读:341      评论:0      收藏:0      [点我收藏+]

/**
* 类名:PropertiesUtil
* 功能:提供对properties配置文件的读取和写入
* @author ChengTao
*/
package com.xy.xyd.rest.biz.service.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public class PropertiesUtil {



/**
* 根据key值查找配置文件里的值
* @param key
* @return
*/
public String getProperty(String key){
      Properties prop = new Properties();
      // URL resource = Thread.currentThread().getContextClassLoader().getResource("");
      InputStream resourceAsStream =

                Thread.currentThread().getContextClassLoader().getResourceAsStream("properties/development/Parameter_xyd.properties");
      try {
            prop.load(resourceAsStream);
            prop.getProperty(key);
      } catch (FileNotFoundException e) {
            e.printStackTrace();
     } catch (IOException e) {
            e.printStackTrace();
      }
     return prop.getProperty(key);
}


/**
* 将文件加载到内存中,在内存中修改key对应的value值,再将文件保存 getFile
* @throws Exception
*/
public void setProper(String key,String value){
      Properties prop = new Properties();
      File file       new File(Thread.currentThread().getContextClassLoader().getResource("properties/development/Parameter_xyd.properties").getFile());      try {

          prop.setProperty(key, value);
          FileOutputStream fos = new FileOutputStream(file);
          prop.store(fos, null);
          fos.close();
     } catch (FileNotFoundException e) {
         e.printStackTrace();
     } catch (IOException e) {
         e.printStackTrace();
      }
}

//inputStream转outputStream
public ByteArrayOutputStream parse(InputStream in) throws Exception{

          ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
          int ch;
          while ((ch = in.read()) != -1) {
              swapStream.write(ch);
          }
         return swapStream;
}


//outputStream转inputStream
public ByteArrayInputStream parse(OutputStream out) throws Exception{
         ByteArrayOutputStream baos=new ByteArrayOutputStream();
         baos=(ByteArrayOutputStream) out;
         ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
         return swapStream;
 }
}

properties配置文件的读取和写入

原文:http://www.cnblogs.com/ctaixw/p/5070881.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!