String,InputStream相互转换
一. InputStream转换为String
转换的过程是:
- 使用FileInputStream读取文件流;
- 使用InputStreamReader读取FileInputStream流;
- 使用BufferedReader读取InputStreamReader;
- 每次读取一行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相互转换的更多相关文章
- String与InputStream相互转换
1.String to InputStream String str = "String与InputStream相互转换"; InputStream in_nocode = ...
- 【转】Drawable /Bitmap、String/InputStream、Bitmap/byte[]
原文:http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/ Drawable互转Bitmap Drawable转Bitmap 1234 Resourc ...
- String inputStream file转化
String --> InputStreamByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); Inp ...
- java常用string inputStream转换
1.String –> InputStream InputStrem is = new ByteArrayInputStream(str.getBytes()); 或者 ByteArrayInp ...
- Java之byte、char和String类型相互转换
package basictype; /** * byte.char和String类型相互转换 */ public class CHJavaType { public static void main ...
- JavaBean与Map<String,Object>相互转换
一.为什么要实现javaBean与Map<String,Object>相互转换 Spring中的BaseCommandController对象可以将传递过来的参数封装到一个JavaBean ...
- QString, Std::string, char *相互转换
Qt 库中对字符串类型进行了封装,QString 类提供了所有字符串操作方法,给开发带来了便利. 由于第三方库的类型基本上都是标准的类型,即使用std::string或char *来表示字符 (串) ...
- C# 跨线程调用form控件技巧及byte[]与string型相互转换
跨线程调用form控件技巧 private delegate void MethodSocket(object obj);//使用托管 ss = "OK"; this.BeginI ...
- Map 集合 和 String 字符串相互转换工具类
package com.skynet.rimp.common.utils.util; import java.util.Arrays; import java.util.HashMap; import ...
随机推荐
- NHibernate初学者指南系列文章导航
NHibernate初学者指南系列文章导航 前面的话 经过三个多周的时间,终于将这个系列完成了,谢谢大家的关注和支持,有很多不足之处还望大家包涵. 本系列参考的书籍为NHibernate 3 Be ...
- ResourceExhaustedError 解决方案
原因:网络层太多,运算量太大导致GPU资源耗尽 解决方案: 1.限制GPU的使用: config = tf.ConfigProto()config.gpu_options.per_process_gp ...
- Lua 判断表是否为空方法
[1]判断表为空的方法 目前为止,Lua语言中判断table表是否为空有三种方式: (1)#table,当table为数组时直接返回table表的长度. (2)当table是字典时,返回table的长 ...
- 【爬虫】biqukan抓取2.0版
#!python3.7 import requests,sys,time,logging,random from lxml import etree logging.basicConfig(level ...
- Java锁详解
http://blog.csdn.net/pzasdq/article/details/53128331 http://blog.csdn.net/truelove12358/article/deta ...
- Linux基础命令---文本编辑ex
ex ex会启动vim编辑器,它的执行效果和vim –E相同.从ex模式回到普通模式,可以在vim中输入:vim. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.op ...
- php 网站301重定向设置代码实战案例
php 网站301重定向设置代码实战案例 301重定向就是页面永久性移走的意思,搜索引擎知道这个页面是301重定向的话,就会把旧的地址替换成重定向之后的地址. 302重定向就是页面暂时性转移,搜索引擎 ...
- AtCoder Beginner Contest 070 ABCD题
题目链接:http://abc070.contest.atcoder.jp/assignments A - Palindromic Number Time limit : 2sec / Memory ...
- how to backup your system of Autel MS908 Pro
how to backup your system of Autel Scan Tool Autel MS908 Pro: Connect the tablet to a PC desktop or ...
- Cent Linux启动tomcat慢的问题
Tomcat7的session id的生成主要通过java.security.SecureRandom生成随机数来实现,随机数算法使用的是”SHA1PRNG”. 是因为一个JDK一个bug,在这个bu ...