博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
properties配置文件的读取和写入
阅读量:4356 次
发布时间:2019-06-07

本文共 2093 字,大约阅读时间需要 6 分钟。

/**

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

转载于:https://www.cnblogs.com/ctaixw/p/5070881.html

你可能感兴趣的文章
Python 的语言特性
查看>>
使用PowerShell批量注册DLL到GAC
查看>>
微软职位内部推荐-Senior Development Engineer
查看>>
创建数据库的方法
查看>>
递归算法
查看>>
关于java中sendRedirect,forward和include区别
查看>>
在红帽RHEL7.0里配置网卡的四种方法
查看>>
LeetCode--二分查找相关算法
查看>>
RobotFramework自动化测试框架系统关键字之断言
查看>>
《Node.js In Action》笔记之流程控制
查看>>
通俗易懂云计算
查看>>
个人笔记 - Word2013 论文格式调整
查看>>
Python学习---Python下[字典]的学习
查看>>
Python学习---重点模块之xml
查看>>
Python学习---django之admin简介
查看>>
个人工作总结11(第二阶段)
查看>>
配置完IDEA开发lua后用idea竟然打不开lua的文件。
查看>>
synchronized、锁、多线程同步的原理是咋样
查看>>
AutoHotKey 快速入门
查看>>
sharepoint 2010批量导入数据
查看>>