zoukankan      html  css  js  c++  java
  • Java中InputStream和String之间的转换方法

    1、InputStream转化为String
    1.1 JDK原生提供
    方法一:
    byte[] bytes = new byte[0];
    bytes = new byte[inputStream.available()];
    inputStream.read(bytes);
    String str = new String(bytes);
    1
    2
    3
    4
    5
    方法二:
    String result = new BufferedReader(new InputStreamReader(inputStream))
    .lines().collect(Collectors.joining(System.lineSeparator()));
    1
    2
    3
    方法三:
    String result = new BufferedReader(new InputStreamReader(inputStream))
    .lines().parallel().collect(Collectors.joining(System.lineSeparator()));
    1
    2
    3
    方法四:
    Scanner s = new Scanner(inputStream).useDelimiter("\A");
    String str = s.hasNext() ? s.next() : "";
    1
    2
    3
    方法五:
    String resource = new Scanner(inputStream).useDelimiter("\Z").next();
    return resource;
    1
    2
    3
    方法六:
    StringBuilder sb = new StringBuilder();
    String line;

    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
    while ((line = br.readLine()) != null) {
    sb.append(line);
    }
    String str = sb.toString();
    return str;
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    方法七:
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) != -1) {
    result.write(buffer, 0, length);
    }
    String str = result.toString(StandardCharsets.UTF_8.name());
    return str;
    1
    2
    3
    4
    5
    6
    7
    8
    9
    方法八:
    BufferedInputStream bis = new BufferedInputStream(inputStream);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    int result = bis.read();
    while(result != -1) {
    buf.write((byte) result);
    result = bis.read();
    }
    String str = buf.toString();
    return str;
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    1.2 Apache Common提供
    方法九:
    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer, StandardCharsets.UTF_8.name());
    String str = writer.toString();
    1
    2
    3
    4
    方法十:
    String str = IOUtils.toString(inputStream, "utf-8");
    1
    2
    1.3 Google Guava提供
    方法十一:
    String str = CharStreams.toString(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
    1
    2
    方法十二:
    String str = new String(ByteStreams.toByteArray(inputStream));
    1
    2
    针对一个2MB的文件的输入流,多次执行测试如下(单位是毫秒):

    方法十: 111
    方法十一: 236
    方法十二: 36
    方法一: 36
    方法二: 87
    方法三: 66
    方法四: 101
    方法五: 178
    方法六: 40
    方法七: 21
    方法八: 107
    方法九: 31

    从上述结果来看,方法七和方法九更好一些,而方法五和方法十一会更差一些。

    2、String转化为InputStream
    2.1 JDK原生提供
    InputStream is = new ByteArrayInputStream(str.getBytes());
    1
    2.2 Apache Common提供
    InputStream targetStream = IOUtils.toInputStream(str, StandardCharsets.UTF_8.name());
    1
    2.3 Google Guava提供
    InputStream targetStream =
    new ReaderInputStream(CharSource.wrap(str).openStream(), StandardCharsets.UTF_8.name());
    ---------------------
    作者:lmy86263
    来源:CSDN
    原文:https://blog.csdn.net/lmy86263/article/details/60479350

  • 相关阅读:
    appium+python 通信原理(下)
    appium+python 通信原理(上)
    百度地图自己添加 标识地点 代码
    让织梦CMS的后台编辑器支持优酷视频
    织梦dede标签tags的美化教程
    ECharts 是一款开源
    dede 留言板访问的目录
    数据库简单的查询
    DEDE在下载文件时会生成table
    JS移动客户端--触屏滑动事件
  • 原文地址:https://www.cnblogs.com/luizw/p/10844105.html
Copyright © 2011-2022 走看看