java中outputStream与inputStream的相互转换

/*做一个功能验证,要用到inputStream与outputStream的转换,本以为很简单的东东
搞了蛮久,从"程序员 闫帆"处取得一段代码*/
package
com.boco.test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; public class ConvertUtil { //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; } //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)); } }

 

posted @ 2013-01-13 21:07  vigarbuaa  阅读(43257)  评论(0编辑  收藏  举报