一. 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. sqli-labs(七)——登陆处sql注入

    第十三关: 这关也是一个登陆口,和上关所说的一样,先使用'"试一下,让程序报错然后判断后台的sql语句 可以看到后台sql大概是 where name = ('$name')...  这样的 ...

  2. Hibernate.编写xml文件无自动提示信息

    Hibernate.编写xml文件无自动提示信息 注意: 配置 xxxx.hbm.xml 文件的自动提示.和配置 hibernate.cfg.xml 文件的提示,操作步骤是一样的.只是复制的文件内容. ...

  3. python 定义函数 调用函数

    创建test.py文件 #coding=utf-8 #定义函数 def hello(): print "hello world" #调用函数 hello() 在cmd下面运行

  4. CentOS双机中Docker下安装Mysql并配置互为主从模式

    CentOS双机中Docker下安装Mysql并配置互为主从模式 目录 1.搜索镜像... 1 2.拉取镜像... 1 3.绑定端口: 1 4.配置文件(修改/etc/mysql/my.cnf文件): ...

  5. IIS 8.0 Using ASP.NET 3.5 and ASP.NET 4.5微软官方安装指导

    from:https://www.iis.net/learn/get-started/whats-new-in-iis-8/iis-80-using-aspnet-35-and-aspnet-45 S ...

  6. SQL中的replace函数

    REPLACE 用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式. 语法 REPLACE ( 'string_expression1' , 'string_expression2 ...

  7. Hello py

    https://www.cnblogs.com/AdaminXie/p/8339863.html https://www.cnblogs.com/-clq/p/8340515.html https:/ ...

  8. NetSpeed

    NetSpeed公司提供的NOC包括三部分,可以通过NocStudio进行配置生成. 1)NetSpeed Orion,面向快速SoC design的可综合平台. 2)Linley NetSpeed ...

  9. centos中yum命令删除还原的补救方法介绍

    前言 yum,是Yellow dog Updater Modified的简称,起初是由yellow dog这一发行版的开发者Terra Soft研发,用python写成,那时还叫做yup(yellow ...

  10. Lua语言总结

    [1]要退出交互模式和解释器,只需输入“os.exit()” [2]在交互模式执行程序块可以使用函数dofile,这个函数就可以立即执行一个文件.应用示例:dofile("f:/myLua/ ...