String和inputstream互转

2018-08-07 19:59:56

URLConnection urlConn = url.openConnection(); // 打开网站链接s
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), "UTF-8")); // 实例化输入流,并获取网页代码
String s; // 依次循环,至到读的值为空
StringBuilder sb = new StringBuilder();
while ((s = reader.readLine()) != null) {
sb.append(s);
}
reader.close();

String str = sb.toString();

 

====================下面的方法有点恶心,改了改,看起来好多了===========================

 

原文:http://blog.csdn.net/soundtravel/article/details/6927006

 

String   str   =   "";//add   your   string   content

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

 

 

 1 package org.kodejava.example.io;
2
3  import java.io.ByteArrayInputStream;
4  import java.io.InputStream;
5
6  publicclass StringToStream {
7 publicstaticvoid main(String[] args) {
8        String text ="Converting String to InputStream Example";
9        
10 /*
11         * Convert String to InputString using ByteArrayInputStream class.
12         * This class constructor takes the string byte array which can be
13         * done by calling the getBytes() method.
14 */
15 try {
16            InputStream is =new ByteArrayInputStream(text.getBytes("UTF-8"));
17        } catch (UnsupportedEncodingException e) {
18            e.printStackTrace();
19        }
20    }
21 }
22  

 

 

 

 

 

1、字符串转inputStream

 

Java代码  收藏代码

  1. String string;  

  2. //......  

  3. InputStream is = new ByteArrayInputStream(string.getBytes());  

 

2、InputStream转字符串

 

Java代码  收藏代码

  1. ByteArrayOutputStream baos = new ByteArrayOutputStream();  

  2. int i;  

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

  4.     baos.write(i);  

  5. }  

  6. String str = baos.toString();  

  7. System.out.println(str);  

 

3、String写入OutputStream

 

Java代码  收藏代码

  1. OutputStream os = System.out;  

  2. os.write(string.getBytes());  

 

4、OutputStream写入String

 

这听起来有点荒谬,OutputStream本来就是输出源,还写入String?

不过最近项目里确实遇到了个类似的问题,比如 SOAPMessage.writeTo(OutputStream os) 这个方法,是将SOAPMessage的内容写到一个输出流中,而我想得到这个流的内容,总不能把他先写进文件再去读这个文件吧,研究了好半天,终于想起可以如下这般:

 

Java代码  收藏代码

  1. ByteArrayOutputStream baos = new ByteArrayOutputStream();  

  2. //向OutPutStream中写入,如 message.writeTo(baos);  

  3. String str = baos.toString();  

 

 

 

将InputStream/OutputStream转换成string

 

这里需要用到一个特殊的类ByteArrayOutputStream,利用他,我们可以将输出流在内存中直接转换成String类型。

具体代码如下:

 

首先从输入流中将数据读出来写入ByteArrayOutputStream,然后再将其转换成String.

 

Java代码  收藏代码

  1. InputStream in = urlconn.getInputStream();//获取输入流  

  2.   

  3. ByteArrayOutputStream bos = new ByteArrayOutputStream();  

  4.   

  5. //读取缓存  

  6. byte[] buffer = new byte[2048];  

  7. int length = 0;  

  8. while((length = in.read(buffer)) != -1) {  

  9.     bos.write(buffer, 0, length);//写入输出流  

  10. }  

  11. in.close();//读取完毕,关闭输入流  

  12.   

  13. // 根据输出流创建字符串对象  

  14. new String(bos.toByteArray(), "UTF-8");  

  15. //or  

  16. //bos.toString("UTF-8");  

 

根据同样的原理,我们可以将outputstream直接转换成String对象。

 

 

指定一下字符集
byte[] b = str.getBytes("utf-8");
String s = new String(b,"utf-8");

 

 

OUTPUTSTREAM中方法WRITE用法

 

 void write(byte[] b) 
          将 b.length 个字节从指定的 byte 数组写入此输出流。 
 void write(byte[] b, int off, int len) 
          将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。 
abstract  void write(int b) 
          将指定的字节写入此输出流。
  • 2017-02-09 09:02:26

    两列布局——左侧宽度固定,右侧宽度自适应的两种方法

     关于左侧宽度固定,右侧宽度自适应两列布局的一种很常用的方法我相信大家都知道。就是利用左侧元素浮动,或者绝对定位的方式使其脱离常规文档流,让两个块级元素能够在同一行显示。然后右侧元素 margin-left 的值等于左侧元素宽度,这时右侧元素将紧挨着左侧元素

  • 2017-02-10 15:19:51

    Git:代码冲突常见解决方法

    如果系统中有一些配置文件在服务器上做了配置修改,然后后续开发又新添加一些配置项的时候, 在发布这个配置文件的时候,会发生代码冲突:

  • 2017-02-10 15:24:14

    linux学习之——vim简明教程

    学习 vim 并且其会成为你最后一个使用的文本编辑器。没有比这个更好的文本编辑器了,非常地难学,但是却不可思议地好用。 我建议下面这四个步骤: 存活 感觉良好 觉得更好,更强,更快 使用VIM的超能力

  • 2017-02-10 16:22:13

    git历史记录查询

    查看提交历史:git log 查看提交历史并显示版本间的差异:git log -p 查看指定历史:git log xxx(sha1值) -p 查看提交历史(指定时间):

  • 2017-02-13 17:50:05

    cURL error 60: SSL certificate problem: unable to get local issuer certificate

    Drupal 8 version uses Guzzle Http Client internally, but under the hood it may use cURL or PHP internals. If you installed PHP cURL on your PHP server it typically uses cURL and you may see an exception with error Peer certificate cannot be authenticated with known CA certificates or error code CURLE_SSL_CACERT (60).