一. String 转换为InputStream

String str = "String 与 inputStream转换";

InputStream ins1 = new ByteArrayInputStream(str.getBytes());

InputStream ins2 = new ByteArrayInputStream(str.getBytes("UTF-8"));

二. InputStream 转为 String

方法一:利用bufferedReader

   1: public String convertToString(InputStream is){

   2:         BufferedReader bReader = new BufferedReader(new InputStreamReader(is));

   3:         StringBuffer buffer = new StringBuffer();

   4:         String line = null;

   5:         try {

   6:             while((line = bReader.readLine())!=null){

   7:                 buffer.append(line);

   8:             }

   9:         } catch (IOException e) {

  10:             // TODO Auto-generated catch block

  11:             e.printStackTrace();

  12:         }finally{

  13:             try {

  14:                 bReader.close();

  15:             } catch (IOException e) {

  16:                 // TODO Auto-generated catch block

  17:                 e.printStackTrace();

  18:             }

  19:         }

  20:         return buffer.toString();

  21:     }

方法二:

   1: public String convertToString(InputStream is){

   2:         StringBuffer buffer = new StringBuffer();

   3:         byte[] b = new byte[1024];

   4:         try {

   5:             for(int n; (n = is.read(b))!=-1;)

   6:                 buffer.append(new String(b,0,n));

   7:             return buffer.toString();

   8:         } catch (IOException e) {

   9:             // TODO Auto-generated catch block

  10:             e.printStackTrace();

  11:         }finally{

  12:             try {

  13:                 is.close();

  14:             } catch (IOException e) {

  15:                 // TODO Auto-generated catch block

  16:                 e.printStackTrace();

  17:             }

  18:         }

  19:     }

方法三:

   1: public String convertToString(InputStream is){

   2:         ByteArrayOutputStream os = new ByteArrayOutputStream();

   3:         int i = -1;

   4:         try {

   5:             while((i = is.read())!=-1){

   6:                 os.write(i);

   7:             }

   8:             return os.toString();

   9:         } catch (IOException e) {

  10:             // TODO Auto-generated catch block

  11:             e.printStackTrace();

  12:             return null;

  13:         }finally{

  14:             try {

  15:                 os.close();

  16:             } catch (IOException e) {

  17:                 // TODO Auto-generated catch block

  18:                 e.printStackTrace();

  19:             }

  20:         }

  21:     }

inputStream 与 String 的互相转换的更多相关文章

  1. InputStream和OutputStream与String之间的转换

    //1.字符串转inputstream String str="aaaaa"; InputStream in = new ByteArrayInputStream(str.getB ...

  2. apache.commons.io.IOUtils: 一个很方便的IO工具库(比如InputStream转String)

    转换InputStream到String, 比如 //引入apache的io包 import org.apache.commons.io.IOUtils; ... ...String str = IO ...

  3. 关于InputStream 和String对象之间的相互转换

    代码如下: package com.xin.stream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...

  4. Java中InputStream和String之间的转化

    https://blog.csdn.net/lmy86263/article/details/60479350 在Java中InputStream和String之间的转化十分普遍,本文主要是总结一下转 ...

  5. [技巧篇]19.InputStream与String,Byte之间互转[转载]

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  6. InputStream与String,Byte之间互转

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  7. 将输入流InputStream转换为String

    public static String convertStreamToString(InputStream is) { /* * To convert the InputStream to Stri ...

  8. C#中char[]与string之间的转换

    string 转换成 Char[] string ss = "abcdefg"; char[] cc = ss.ToCharArray(); Char[] 转换成string st ...

  9. C字符串和C++中string的区别 &&&&C++中int型与string型互相转换

    在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别:   C字符串 string对象(C++) 所需的头文件名称 ...

随机推荐

  1. Adventure Works 教程

    多维建模(Adventure Works 教程)     欢迎使用 Analysis Services 教程. 本教程通过在所有示例中使用虚构公司 Adventure Works Cycles,说明如 ...

  2. CSS3:nth-child()伪类选择器,Table表格奇偶数行定义样式

    转自爱设计 原文链接http://www.dangshopex.com/jishufenxiang/WEBkaifajishu/8653.html CSS3的强大,让人惊叹,人们在惊喜之余,又不得不为 ...

  3. HDU - 2612 Find a way 双起点bfs(路径可重叠:两个队列分别跑)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. now code寒假练习赛2——处女座的砝码(找规律题+高精度题)

    #include <bits/stdc++.h> #define ll long long using namespace std; int main() { long double n ...

  5. MFC控件:listctrl使用方法总结

    以下未经说明,listctrl默认view 风格为report 相关类及处理函数 MFC:CListCtrl类 SDK:以 “ListView_”开头的一些宏.如 ListView_InsertCol ...

  6. CodeForces水题

    CodeForces754A 题意: 给一个数组,让你变成1-n,输出变换区间,要求原区间和不为0. 思路: 如果原数组不为0,那就是YES: 如果为0,则从1开始扫过去,碰到不为0时,分两个区间[1 ...

  7. CodeForces722C Destroying Array【瞎搞】

    题意: 先给你一个序列,然后给你n个1-n的一个数,让你求前i个元素销毁的时候,区间字段和区间最大: 思路: 离线处理,维护新区间首尾位置的起点和终点,倒着处理: #include <bits/ ...

  8. 51nod1640(kruscal)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1640 题意:中文题诶- 思路:kruscal 题目要求是在边权 ...

  9. IT兄弟连 JavaWeb教程 Servlet会话跟踪 Session常用方法

    ●  public Object getAttribute(String name) 该方法返回在该session会话中具有指定名称的对象,如果没有指定名称的对象,则返回null. ●  public ...

  10. 【BZOJ1174】: [Balkan2007]Toponyms

    →原题← ↑这样子我就不复制题面啦~ 就是一题很裸的字典树而已,不过空间卡的很死,直接开个数组 tr[N][52] 什么之类的一定会RE的(惨痛的教训) 当字典树空间不够而时间限制又比较宽松时,我们可 ...