Java中String与BufferedReader、InputStream转换

String –> InputStream

1
2
3
InputStrem is = new ByteArrayInputStream(str.getBytes());
//或者
ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());

InputStream –> String

1
2
3
4
5
6
7
8
inputStream input = null;

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

Reader –> String

1
2
3
4
5
6
7
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
buffer.append(line);
}
return buffer.toString();

String –> Reader

1
Reader reader = new StringReader("http://www.appblog.cn");

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :