String与InputStream相互转换
1.String to InputStream
String str = "String与InputStream相互转换";
InputStream in_nocode = new ByteArrayInputStream(str.getBytes());
InputStream in_withcode = new ByteArrayInputStream(str.getBytes("UTF-8"));
2.InputStream to String
这里提供几个方法。
方法1:
public String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "/n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
方法2:
public String inputStream2String (InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
方法3:
public static String inputStream2String(InputStream is) throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i=-1;
while((i=is.read())!=-1){
baos.write(i);
}
return baos.toString();
}
String与InputStream相互转换的更多相关文章
- String,InputStream相互转换
一. InputStream转换为String 转换的过程是: 使用FileInputStream读取文件流: 使用InputStreamReader读取FileInputStream流: 使用Buf ...
- String与InputStream互转的几种方法
[java] view plain copy /** * 利用BufferedReader实现Inputstream转换成String <功能详细描述> * * @param in * @ ...
- String 和 InputStream 互转方式
/** * 利用BufferedReader实现Inputstream转换成String <功能详细描述> * * @param in * @return String */ public ...
- C#中string和byte[]相互转换问题解决
本来想讲string转换为byte数组,通过在VS上打 ‘str. “来找,结果半天没发现跳出来的函数中有想要的,哭瞎 /(ㄒoㄒ)/~~ 这回将两种情况都记下来了.... string ---> ...
- String和inputstream互转【转文】
URLConnection urlConn = url.openConnection(); // 打开网站链接s BufferedReader reader = new BufferedReader( ...
- String与StringBuilder相互转换以及获取字符串中第一个中文汉字
String与StringBuilder相互转换 1. StringBuilder转为String StringBuilder sb = new StringBuilder(); sb.append( ...
- java String与Byte[]和String 与InputStream转换时注意编码问题。。。
前一段日子,我在做rsa加密和通过http get方式获取验证码图片通过BitmapFactory创建bitmap 出现了一系列的问题. 通过一系列的调试,发现有些问题原来是在进行String 与By ...
- String 、InputStream、Reader 的转换
1.String –> InputStream InputStrem is = new ByteArrayInputStream(str.getBytes());orByteArrayInput ...
- 【Java】【2】String和List相互转换
正文: 1,String转List //常见的为逗号分隔 String str = "a,b,c"; List<String> list1 = Arrays.asLis ...
随机推荐
- Filter过滤的2种方式
1.新建一个过滤器,继承ActionFilterAttribute,然后重写 public class DemoFilterAttribute:ActionFilterAttribute { //在A ...
- 【过程改进】 windows下jenkins常见问题填坑
没有什么高深的东西,1 2天的时间大多数人都能自己摸索出来,这里将自己遇到过的问题分享出来避免其他同学再一次挖坑. 目录 1. 主从节点 2. Nuget自动包还原 3. powershell部署 4 ...
- 关于SSIS中解密数据库字符串的方法
此文章适合于SSIS新手,我是个小白,在繁复查阅资料后仍无果到最后解决问题,走了很多弯路,现在讲其中一些关于SSIS的理解写出来,供大家参考,在正文之前,我就我自己的理解,阐明一些概念. 什么是SSI ...
- React,js实现分页的案列
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- java开发环境的主题色的变化
eclipse:Help->Install New Software->Work with:Update Site - http://eclipse-color-theme.github ...
- Hadoop安装指引
pre.ctl { font-family: "Liberation Mono", monospace } p { margin-bottom: 0.25cm; line-heig ...
- [poj3378] Crazy Thairs (DP + 树状数组维护 + 高精度)
树状数组维护DP + 高精度 Description These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ...
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序更新相关数据
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第八篇:为ASP.NET MVC应用程序 ...
- 深入理解CSS网页布局-理论篇
在CSS网页开发布局中,需要对浮动和定位有深刻的理解才能在开发中游刃有余. 基于此,在博客园中做了本篇总结,这些总结来自实践经验和阅读一些书籍后的理解总结,主要内容为浮动,清除浮动,定位. (可点击屏 ...
- HTTP协议的头信息详解
转载地址:http://blog.csdn.net/guoguo1980/article/details/2649658 HTTP(HyperTextTransferProtocol)是超文本传输协议 ...