String与InputStream相互转换
2016-07-07 11:03
375 查看
1.String to InputStream
2.InputStream to String
这里提供几个方法。
public static InputStream String2InputStream(String name) { return new ByteArrayInputStream(name.getBytes()); }
2.InputStream to String
这里提供几个方法。
public static String InputStream2String(InputStream input,String encode) throws IOException { StringBuilder builder = new StringBuilder(); InputStreamReader reader = new InputStreamReader(input,encode); BufferedReader bufferedReader = new BufferedReader(reader); String line = null; try { while ((line = bufferedReader.readLine()) != null) { builder.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { input.close(); } return builder.toString(); } public static String InputStream2String2(InputStream input) throws IOException { StringBuffer buffer = new StringBuffer(); byte[] b = new byte[1024]; int n; try { while ((n = input.read(b)) != -1) { buffer.append(new String(b, 0, n)); } } catch (IOException e) { e.printStackTrace(); } finally { input.close(); } return buffer.toString(); } public static String InputStream2String3(InputStream input) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int i; try { while ((i = input.read()) != -1) { outputStream.write(i); } } catch (IOException e) { e.printStackTrace(); } finally { input.close(); } return outputStream.toString(); }
相关文章推荐
- opensearch的使用
- HMM(参数训练无监督方法EM)
- Android中getWritableDatabase与getReadableDatabase的用法区别
- 使用CollectionUtils.isNotEmpty() 判断 List Set 是否为null
- 析构函数是否必须为虚函数?什么情况下才应该定义析构函数为虚函数?
- mysql时间的一些计算
- android 的四种枚举Context.MODE_PRIVATE
- CSS Z-index属性 和 @keyframes创建动画
- libpcap读取本地文件
- Linux书籍汇总
- 安装tomcat
- iOS 代理委托设计模式
- SpringData JPA 排除 扫描 exclude-filter 不能使用解决
- spark on yarn
- spark-deploy-模块源码分析
- sql复制表结构及内容
- Leetcode Candy
- windows蓝屏代码:stop 0x0000007b蓝屏故障
- collection和collections的区别
- Linux 进程