Java中InputStream和String之间的转换方法

在Java中InputStream和String之间的转化十分普遍,本文主要是总结一下转换的各种方法,包括JDK原生提供的,还有一些外部依赖提供的。

1、InputStream转化为String

1.1 JDK原生提供

 
  1. 方法一:

  2. byte[] bytes = new byte[0];

  3. bytes = new byte[inputStream.available()];

  4. inputStream.read(bytes);

  5. String str = new String(bytes);

  • 1
  • 2
  • 3
  • 4
  • 5
 
  1. 方法二:

  2. String result = new BufferedReader(new InputStreamReader(inputStream))

  3. .lines().collect(Collectors.joining(System.lineSeparator()));

  • 1
  • 2
  • 3
 
  1. 方法三:

  2. String result = new BufferedReader(new InputStreamReader(inputStream))

  3. .lines().parallel().collect(Collectors.joining(System.lineSeparator()));

  • 1
  • 2
  • 3
 
  1. 方法四:

  2. Scanner s = new Scanner(inputStream).useDelimiter("\\A");

  3. String str = s.hasNext() ? s.next() : "";

  • 1
  • 2
  • 3
 
  1. 方法五:

  2. String resource = new Scanner(inputStream).useDelimiter("\\Z").next();

  3. return resource;

  • 1
  • 2
  • 3
 
  1. 方法六:

  2. StringBuilder sb = new StringBuilder();

  3. String line;

  4.  
  5. BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

  6. while ((line = br.readLine()) != null) {

  7. sb.append(line);

  8. }

  9. String str = sb.toString();

  10. return str;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
 
  1. 方法七:

  2. ByteArrayOutputStream result = new ByteArrayOutputStream();

  3. byte[] buffer = new byte[1024];

  4. int length;

  5. while ((length = inputStream.read(buffer)) != -1) {

  6. result.write(buffer, 0, length);

  7. }

  8. String str = result.toString(StandardCharsets.UTF_8.name());

  9. return str;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
 
  1. 方法八:

  2. BufferedInputStream bis = new BufferedInputStream(inputStream);

  3. ByteArrayOutputStream buf = new ByteArrayOutputStream();

  4. int result = bis.read();

  5. while(result != -1) {

  6. buf.write((byte) result);

  7. result = bis.read();

  8. }

  9. String str = buf.toString();

  10. return str;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

1.2 Apache Common提供

 
  1. 方法九:

  2. StringWriter writer = new StringWriter();

  3. IOUtils.copy(inputStream, writer, StandardCharsets.UTF_8.name());

  4. String str = writer.toString();

  • 1
  • 2
  • 3
  • 4
 
  1. 方法十:

  2. String str = IOUtils.toString(inputStream, "utf-8");

  • 1
  • 2

1.3 Google Guava提供

 
  1. 方法十一:

  2. String str = CharStreams.toString(new InputStreamReader(inputStream, StandardCharsets.UTF_8));

  • 1
  • 2
 
  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提供

 
  1. InputStream targetStream =

  2. new ReaderInputStream(CharSource.wrap(str).openStream(), StandardCharsets.UTF_8.name());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值