原创地址:http://www.yihaomen.com/article/java/415.htm
最近在做项目的时候,遇到一个问题,需要在内存中对从不同地方收集起来的 Properties 文件做处理,在处理之后,要合并成一个 单独的 Properties 并输出为 inputStream ,做后续的处理。如果单纯从 properties文件转换成 inputStream 应该是比较容易的事。在内存中处理合并properties 也比较简单,但 从Properties 对象转换成 inputStream 我硬是冤枉了两个小时。很郁闷,不过最后还是找到了方法,其重点就是 通过outputStream 作为中转来实现,参考了网上的一个 inputStream 与 outputStream 与 String 对象之间相互转换的代码,一起写在里面。
package com.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.Properties;
import org.junit.Test;
public class readProperties {
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;
}
//inputStream转String
public String parse_String(InputStream in) throws Exception
{
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
int ch;
while ((ch = in.read()) != -1) {
swapStream.write(ch);
}
return swapStream.toString();
}
//OutputStream 转String
public String parse_String(OutputStream out)throws Exception
{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
baos=(ByteArrayOutputStream) out;
ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
return swapStream.toString();
}
//String转inputStream
public ByteArrayInputStream parse_inputStream(String in)throws Exception
{
ByteArrayInputStream input=new ByteArrayInputStream(in.getBytes());
return input;
}
//String 转outputStream
public ByteArrayOutputStream parse_outputStream(String in)throws Exception
{
return parse(parse_inputStream(in));
}
@Test
public void getProperties() throws Exception{
//test.properties文件默认在src/config目录中下;
URL url = getClass().getClassLoader().getResource("config/test.properties");
InputStreamReader inputStreamReader = null;
Properties properties = new Properties();
try {
InputStream inputStream = url.openStream();
inputStreamReader = new InputStreamReader(inputStream);
//加载配置文件;
properties.load(inputStreamReader);
//根据key获取value;
properties.setProperty("aaa", "modify password");
String value = properties.getProperty("bbb");
System.out.println(value);
properties.setProperty("bbb", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
OutputStream bos = new ByteArrayOutputStream();
properties.store(bos, "");
//重新转换成流inputStream
inputStream = parse(bos);
properties.clear();
inputStreamReader = new InputStreamReader(inputStream);
properties.load(inputStreamReader);
System.out.println( properties.getProperty("aaa") );
System.out.println( properties.getProperty("bbb") );
inputStreamReader.close();
inputStream.close();
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这样就能实现properties 文件与 inputStream ,outputStream ,String ,甚至文件之间的任意转换了。