码迷,mamicode.com
首页 > 编程语言 > 详细

java常用string inputStream转换

时间:2016-09-07 19:01:24      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

1、String –> InputStream

 

InputStrem is = new ByteArrayInputStream(str.getBytes());
或者
ByteArrayInputStream stream= new ByteArrayInputStream(str.getBytes());

 

 2、InputStream–>String

 

inputStream input;

StringBuffer out = new StringBuffer();
     byte[] b = new byte[4096];
     for (int n; (n = input.read(b)) != -1;) {
          out.append(new String(b, 0, n));
     }
out.toString();

 

3、Reader –>String

 

BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = " ";
while ((line = in.readLine()) != null){
     buffer.append(line);
}
return buffer.toString();

 

4、String–>Reader

 

Reader reader = null;
BufferedReader r = new BufferedReader(reader);
StringBuilder b = new StringBuilder();
String line;
while((line=r.readLine())!=null) {
     b.append(line);
     b.append(“\r\n”);
}
b.toString();

 

java常用string inputStream转换

标签:

原文地址:http://www.cnblogs.com/chenmo-xpw/p/5850391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!