lhi705的专栏

个人资料整理...

InputStream、OutputStream、String的相互转换

1、String > InputStream
//字符串转inputStream
   InputStream is =new ByteArrayInputStream(ins.getBytes());
   //InputStream转字符串
   byte[] b = new byte[ins.getBytes().length];
   is.read(b);
   System.out.println(new String(b));
   System.out.println("ok");
   is.close();
String string;
//......
InputStream is = new ByteArrayInputStream(string.getBytes());  
2、InputStream > String

ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i;
while ((i = is.read()) != -1) {
	baos.write(i);
}
String str = baos.toString();
System.out.println(str);  
3、String > OutputStream

OutputStream os = System.out;
os.write(string.getBytes()); 
4、OutputStream > String

ByteArrayOutputStream baos = new ByteArrayOutputStream();//向OutPutStream中写入,如 message.writeTo(baos);
String str = baos.toString();


 

阅读更多
文章标签: string byte os
个人分类: JavaSE
上一篇Jquery...
下一篇JSTL ---core functions formatdata
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭