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

  • 相关阅读:
    POJ 3713 Transferring Sylla【Tarjan求割点】
    Tarjan算法【强连通分量】
    POJ 1273 Drainage Ditches【最大流模版】
    CDOJ 1960 构造哈密顿路径
    HDU 1384 Intervals【差分约束-SPFA】
    POJ 1364 / HDU 3666 【差分约束-SPFA】
    【SPFA与Dijkstra的对比】CDOJ 1961 咸鱼睡觉觉【差分约束-负权最短路径SPFA】
    CDOJ 1965 连通域统计【DFS】
    CDOJ 1964 命运石之门【最短路径Dijkstra/BFS】
    最小生成树模板【kruskal & prim】
  • 原文地址:https://www.cnblogs.com/ctaixw/p/5070881.html
Copyright © 2011-2022 走看看