inputStream to Properties 与 Properties to 流

摘要: 最近在做项目的时候,遇到一个问题,需要在内存中对从不同地方收集起来的 Properties 文件做处理,在处理之后,要合并成一个 单独的 Properties 并输出为 inputStream ,做后续的处理。如果单纯从 properties文件转换成 inputStream 应该是比较容易的事。在内存中处理合并properties 也比较简单,但 从Properties 对象转换成 inputStream 我硬是冤枉了两个小时。很郁闷,不过最后还是找到了方法,其重点就是 通过outputStream 作为中转来实现,参考了网上的一个 inputStream 与 outputStream 与 String 对象之间相互转换的代码,一起写在里面.

最近在做项目的时候,遇到一个问题,需要在内存中对从不同地方收集起来的 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 ,甚至文件之间的任意转换了。

上一篇: javascript异步处理与Jquery的deferred对象总结
下一篇: 最适合中国国情的jquery file upload 批量上传改版插件,结合spring mvc
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号