InputStream和OutputStream与String之间的转换
//1.字符串转inputstream
String str="aaaaa";
InputStream in = new ByteArrayInputStream(str.getBytes()); //2.inputstream转字符串
String result = readFromInputStream(inputStream);//调用处
//将输入流InputStream变为String
public String readFromInputStream(InputStream in) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
baos.close();
in.close(); byte[] lens = baos.toByteArray();
String result = new String(lens,"UTF-8");//内容乱码处理 return result; }
//3.String写入OutputStream中
OutputStream out = System.out;
out.write(str.getBytes()); //4.outputStream转string
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//向OutPutStream中写入,如 message.writeTo(baos);
baos.write(str.getBytes());
String str1= baos.toString();
InputStream和OutputStream与String之间的转换的更多相关文章
- 如何在Byte[]和String之间进行转换
源自C#与.NET程序员面试宝典. 如何在Byte[]和String之间进行转换? 比特(b):比特只有0 1,1代表有脉冲,0代表无脉冲.它是计算机物理内存保存的最基本单元. 字节(B):8个比特, ...
- c# String ,String[] 和 List<String>之间的转换
C#对字符串进行处理时,经常需要进行String,String[]和List<String>之间的转换 本文分析一下它们的差异和转换 一. 1. String > String[] ...
- 基本数据类型、包装类、String之间的转换
package 包装类; /** *8种基本数据类型对应一个类,此类即为包装类 * 基本数据类型.包装类.String之间的转换 * 1.基本数据类型转成包装类(装箱): * ->通过构造器 : ...
- java字符数组char[]和字符串String之间的转换
java字符数组char[]和字符串String之间的转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 使用String.valueOf()将字符数组转换成字符串 void (){ cha ...
- java中Integer 和String 之间的转换
java中Integer 和String 之间的转换 将数组转换成字符串:char[] array = {'a','b','c','d','e'};String str = new String(ar ...
- C#中char[]与string之间的转换;byte[]与string之间的转化
目录 1.char[]与string之间的转换 2.byte[]与string之间的转化 1.char[]与string之间的转换 //string 转换成 Char[] string str=&qu ...
- char* 、const char*和string之间的转换
1. const char* 和string 转换 (1) const char*转换为 string,直接赋值即可. EX: const char* tmp = "tsinghua ...
- C#中char[]与string之间的转换
string 转换成 Char[] string ss = "abcdefg"; char[] cc = ss.ToCharArray(); Char[] 转换成string st ...
- InputStream、OutputStream、String的相互转换(转)
//1.字符串转inputStream String string; //...... InputStream is = new ByteArrayInputStream(string.getByte ...
随机推荐
- 使用SSL确保通信中的数据安全
#region Server /// <summary> /// 用于保存非对称加密(数字证书)的公钥 /// </summary> private string public ...
- projecteuler Sum square difference
The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of ...
- Windows Phone 开发起步之旅之一 平台环境的搭建
最近大家都在写博客园的技术文章,按耐不住了,也把自己平时学习中遇到和学习到的一些东西写出来,供大家分享也好,自己留个纪念也好,有个可以查看的东西. 言归正传,随着微软对Windows Phone的投入 ...
- 《深入剖析Tomcat》读书笔记(二)
三.容器Container Container 是容器的父接口,所有子容器都必须实现这个接口.Container 容器的设计用的是典型的责任链的设计模式,它有四个子容器组件构成,分别是:Engine. ...
- crontab的使用说明
网上瞎转载的,仅供参考 名称 : crontab 使用权限 : 所有使用者 使用方式 : crontab file [-u user]-用指定的文件替代目前的crontab. crontab-[-u ...
- Oracle中存储过程与函数的区别
Oracle 获取信息一般用function 修改数据用存储过程(需要执行commit命令)
- 用户 'sa' 登录失败。 (Microsoft SQL Server,错误: 18456)
今天登陆数据库的时候,却忽然登陆了不了,并且提示了这样的错: 解决方法: 1.用Windows身份登录数据库 2.安全性==>登录名==>双击sa 3.重设密码 4.状态==>登录: ...
- 学习c语言的第9天
#include <stdio.h> int main() { float sum=0,wage=0; int i=1; int num; printf("+++平均工资统计程序 ...
- php获取图片宽高等属性
<?php function getImageInfo($image) { $imageInfo = getimagesize($image); if ($imageInfo ! ...
- 使用了Theme但是没有效果问题
最近在开发过程中使用了theme移植Preference并使用了一些android样式,但是在自定义的Theme修改了相关参数后却无法实现 可能有些朋友还不知道怎么用.这里也做个简要的使用方式说明. ...