Java中String与BufferedReader、InputStream转换

String –> InputStream

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

InputStream –> String

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

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

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

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/13/java-string-bufferedreader-inputstream/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Java中String与BufferedReader、InputStream转换
String –> InputStream InputStrem is = new ByteArrayInputStream(str.getBytes()); //或者 ByteArrayInputStream stream = new ByteArrayInputStream(str……
<<上一篇
下一篇>>
文章目录
关闭
目 录