一. InputStream转换为String

转换的过程是:

  1. 使用FileInputStream读取文件流;
  2. 使用InputStreamReader读取FileInputStream流;
  3. 使用BufferedReader读取InputStreamReader;
  4. 每次读取一行BufferedReader,遍历。

具体代码如下:

String template="D;//test.txt";
FileInputStream fileInputStream=null;
InputStream in=null;
BufferedReader tBufferedReader=null;
StringBuffer tStringBuffer=new StringBuffer();//转换为的字符串
try {
fileInputStream = new FileInputStream(template);
tBufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String sTempOneLine = new String("");
while ((sTempOneLine = tBufferedReader.readLine()) != null){
tStringBuffer.append(sTempOneLine);
}
}catch(Exception e){
e.printStackTrace();
} finally{
try {
tBufferedReader.close();
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

二. String转换为InputStream

转换过程需要借助ByteArrayInputStream读取字符串的字节码,ByteArrayInputStream是InputStream的子类,强制转换即可。

代码如下:

String template="abcdef";
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(template.getBytes());
InputStream inputStream=(InputStream)byteArrayInputStream;

String,InputStream相互转换的更多相关文章

  1. String与InputStream相互转换

    1.String to InputStream String str = "String与InputStream相互转换"; InputStream   in_nocode   = ...

  2. 【转】Drawable /Bitmap、String/InputStream、Bitmap/byte[]

    原文:http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/ Drawable互转Bitmap Drawable转Bitmap 1234 Resourc ...

  3. String inputStream file转化

    String --> InputStreamByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); Inp ...

  4. java常用string inputStream转换

    1.String –> InputStream InputStrem is = new ByteArrayInputStream(str.getBytes()); 或者 ByteArrayInp ...

  5. Java之byte、char和String类型相互转换

    package basictype; /** * byte.char和String类型相互转换 */ public class CHJavaType { public static void main ...

  6. JavaBean与Map<String,Object>相互转换

    一.为什么要实现javaBean与Map<String,Object>相互转换 Spring中的BaseCommandController对象可以将传递过来的参数封装到一个JavaBean ...

  7. QString, Std::string, char *相互转换

    Qt 库中对字符串类型进行了封装,QString 类提供了所有字符串操作方法,给开发带来了便利. 由于第三方库的类型基本上都是标准的类型,即使用std::string或char *来表示字符 (串) ...

  8. C# 跨线程调用form控件技巧及byte[]与string型相互转换

    跨线程调用form控件技巧 private delegate void MethodSocket(object obj);//使用托管 ss = "OK"; this.BeginI ...

  9. Map 集合 和 String 字符串相互转换工具类

    package com.skynet.rimp.common.utils.util; import java.util.Arrays; import java.util.HashMap; import ...

随机推荐

  1. Bukkit插件编程之检测玩家受到的伤害是来自投射类武器还是近身武器

    package com.sklm.lhb.listener; import org.bukkit.entity.Arrow; import org.bukkit.entity.Player; impo ...

  2. linux 下安装mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar

    -rw-rw-r--. hadoop hadoop Nov : mysql--.el6.x86_64.rpm-bundle.tar tar -xvf mysql-5.7.12-1.el6.x86_64 ...

  3. hbase-java-api002(flush)

    package api; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apa ...

  4. sqlserver Distributed Transaction 分布式事务

    在webapi+ef+sqlserver开发项目时,利用transcope实现应用层级的事务时,偶尔会报分布式事务错误,而且很而复现,特别蛋疼.现将自己的解决方法初步整理下. 分析原因:搭建repos ...

  5. pip install pyinstaller

    C:\Users\coder211\Desktop>pip install pyinstallerCollecting pyinstaller Downloading PyInstaller-3 ...

  6. linux正则

    正则表达式 分两类:  基本正则表达式:BRE  扩展正则表达式:ERE :grep -E, egrep 正则表达式引擎:  采用不同算法,检查处理正则表达式的软件模块       PCRE(Perl ...

  7. C# vs2015单元测试测试资源管理器不显示测试方法

    问题描述:在用VS2015用测试框架NUnit单元测试的时候,测试资源管理器死活不出现测试方法,无法运行单元测试模块 现象如下图: 原因:nunit版本不对应 解决方案:下载nunit3.0及往上的版 ...

  8. rest-framework 序列化格式Restful API设计规范

    理解RESTful架构 Restful API设计指南 理解RESTful架构 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式 ...

  9. volatile 线程内存模型

  10. 【函数封装】javascript判断是否是微信浏览器

    //判断是否是微信浏览器的函数 function isWeiXin(){ //window.navigator.userAgent属性包含了浏览器类型.版本.操作系统类型.浏览器引擎类型等信息,这个属 ...