zoukankan      html  css  js  c++  java
  • properties配置文件的读取和写入

    /**
    * 类名: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;
     }
    }

  • 相关阅读:
    ASP.NET存储过程自定义分页详解
    ajax php POST 提交例子
    一个用存储过程的基本分页及其调用
    DataGrid 存储过程的分页
    无刷新无限级菜单联动
    asp.net URL多参数傳值以及特殊符号传值问题
    ASP.NET页面间参数的传递
    Android动画开发——Animation动画效果
    android surface
    Android控件属性——android:cacheColorHint=“#00000000”
  • 原文地址:https://www.cnblogs.com/ctaixw/p/5070881.html
Copyright © 2011-2022 走看看