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