inputStream to Properties 与 Properties to 流

原创地址: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 ,甚至文件之间的任意转换了。

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值