java常用string inputStream转换
Posted 坚守一辈子的幸福
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java常用string inputStream转换相关的知识,希望对你有一定的参考价值。
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转换的主要内容,如果未能解决你的问题,请参考以下文章
java中,如何将InputStream类型的东东转成FileInputStream类型?
java 中inputstream流 转成string,再通过String进行文件的读写时错误