如果你有一个java.io.InputStream
对象,你应该如何处理该对象并产生一个String
?
假设我有一个InputStream
包含文本数据,并且我想将其转换为a String
,所以例如我可以将其写入日志文件.
采取InputStream
并将其转换为最简单的方法是String
什么?
public String convertStreamToString(InputStream is) { // ??? }
Harry Lime.. 2424
一个很好的方法是使用Apache公共资源 IOUtils
将其复制InputStream
到StringWriter
...类似的东西
StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, encoding); String theString = writer.toString();
甚至
// NB: does not close inputStream, you'll have to use try-with-resources for that String theString = IOUtils.toString(inputStream, encoding);
或者,ByteArrayOutputStream
如果您不想混合Streams和Writers ,则可以使用
一个很好的方法是使用Apache公共资源 IOUtils
将其复制InputStream
到StringWriter
...类似的东西
StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, encoding); String theString = writer.toString();
甚至
// NB: does not close inputStream, you'll have to use try-with-resources for that String theString = IOUtils.toString(inputStream, encoding);
或者,ByteArrayOutputStream
如果您不想混合Streams和Writers ,则可以使用
这是一种仅使用标准Java库的方法(请注意,流未关闭,您的里程可能会有所不同).
static String convertStreamToString(java.io.InputStream is) { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; }
我从"Stupid Scanner tricks"文章中学到了这个技巧.它工作的原因是因为Scanner迭代流中的标记,在这种情况下我们使用"输入边界的开头"(\ A)分隔标记,因此只为流的整个内容提供一个标记.
注意,如果您需要具体了解输入流的编码,可以为Scanner
构造函数提供第二个参数,指示要使用的字符集(例如"UTF-8").
帽子小贴士也向Jacob说道,他曾经指着我说过这篇文章.
总结其他答案我找到了11种主要方法(见下文).我写了一些性能测试(见下面的结果):
将InputStream转换为String的方法:
使用IOUtils.toString
(Apache Utils)
String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
使用CharStreams
(番石榴)
String result = CharStreams.toString(new InputStreamReader( inputStream, Charsets.UTF_8));
使用Scanner
(JDK)
Scanner s = new Scanner(inputStream).useDelimiter("\\A"); String result = s.hasNext() ? s.next() : "";
使用Stream API(Java 8).警告:此解决方案将不同的换行符(如\r\n
)转换为\n
.
String result = new BufferedReader(new InputStreamReader(inputStream)) .lines().collect(Collectors.joining("\n"));
使用并行Stream API(Java 8).警告:此解决方案将不同的换行符(如\r\n
)转换为\n
.
String result = new BufferedReader(new InputStreamReader(inputStream)).lines() .parallel().collect(Collectors.joining("\n"));
使用InputStreamReader
和StringBuilder
(JDK)
final int bufferSize = 1024; final char[] buffer = new char[bufferSize]; final StringBuilder out = new StringBuilder(); Reader in = new InputStreamReader(inputStream, "UTF-8"); for (; ; ) { int rsz = in.read(buffer, 0, buffer.length); if (rsz < 0) break; out.append(buffer, 0, rsz); } return out.toString();
使用StringWriter
和IOUtils.copy
(Apache Commons)
StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, "UTF-8"); return writer.toString();
使用ByteArrayOutputStream
和inputStream.read
(JDK)
ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } // StandardCharsets.UTF_8.name() > JDK 7 return result.toString("UTF-8");
使用BufferedReader
(JDK).警告:此解决方案将不同的换行符(例如\n\r
)转换为line.separator
系统属性(例如,在Windows中转换为"\ r \n").
String newLine = System.getProperty("line.separator"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder result = new StringBuilder(); boolean flag = false; for (String line; (line = reader.readLine()) != null; ) { result.append(flag? newLine: "").append(line); flag = true; } return result.toString();
使用BufferedInputStream
和ByteArrayOutputStream
(JDK)
BufferedInputStream bis = new BufferedInputStream(inputStream); ByteArrayOutputStream buf = new ByteArrayOutputStream(); int result = bis.read(); while(result != -1) { buf.write((byte) result); result = bis.read(); } // StandardCharsets.UTF_8.name() > JDK 7 return buf.toString("UTF-8");
使用inputStream.read()
和StringBuilder
(JDK).警告:此解决方案存在Unicode问题,例如使用俄语文本(仅适用于非Unicode文本)
int ch; StringBuilder sb = new StringBuilder(); while((ch = inputStream.read()) != -1) sb.append((char)ch); reset(); return sb.toString();
警告:
解决方案4,5和9将不同的换行符转换为1.
解决方案11无法与Unicode文本一起正常工作
性能测试
github中小String
(长度= 175),url的性能测试(模式=平均时间,系统= Linux,得分1,343是最好的):
Benchmark Mode Cnt Score Error Units
8. ByteArrayOutputStream and read (JDK) avgt 10 1,343 ± 0,028 us/op
6. InputStreamReader and StringBuilder (JDK) avgt 10 6,980 ± 0,404 us/op
10. BufferedInputStream, ByteArrayOutputStream avgt 10 7,437 ± 0,735 us/op
11. InputStream.read() and StringBuilder (JDK) avgt 10 8,977 ± 0,328 us/op
7. StringWriter and IOUtils.copy (Apache) avgt 10 10,613 ± 0,599 us/op
1. IOUtils.toString (Apache Utils) avgt 10 10,605 ± 0,527 us/op
3. Scanner (JDK) avgt 10 12,083 ± 0,293 us/op
2. CharStreams (guava) avgt 10 12,999 ± 0,514 us/op
4. Stream Api (Java 8) avgt 10 15,811 ± 0,605 us/op
9. BufferedReader (JDK) avgt 10 16,038 ± 0,711 us/op
5. parallel Stream Api (Java 8) avgt 10 21,544 ± 0,583 us/op
大String
(长度= 50100)的性能测试,github中的 url (模式=平均时间,系统= Linux,得分200,715是最好的):
Benchmark Mode Cnt Score Error Units
8. ByteArrayOutputStream and read (JDK) avgt 10 200,715 ± 18,103 us/op
1. IOUtils.toString (Apache Utils) avgt 10 300,019 ± 8,751 us/op
6. InputStreamReader and StringBuilder (JDK) avgt 10 347,616 ± 130,348 us/op
7. StringWriter and IOUtils.copy (Apache) avgt 10 352,791 ± 105,337 us/op
2. CharStreams (guava) avgt 10 420,137 ± 59,877 us/op
9. BufferedReader (JDK) avgt 10 632,028 ± 17,002 us/op
5. parallel Stream Api (Java 8) avgt 10 662,999 ± 46,199 us/op
4. Stream Api (Java 8) avgt 10 701,269 ± 82,296 us/op
10. BufferedInputStream, ByteArrayOutputStream avgt 10 740,837 ± 5,613 us/op
3. Scanner (JDK) avgt 10 751,417 ± 62,026 us/op
11. InputStream.read() and StringBuilder (JDK) avgt 10 2919,350 ± 1101,942 us/op
图表(性能测试取决于Windows 7系统中的输入流长度)
性能测试(平均时间)取决于Windows 7系统中的输入流长度:
length 182 546 1092 3276 9828 29484 58968
test8 0.38 0.938 1.868 4.448 13.412 36.459 72.708
test4 2.362 3.609 5.573 12.769 40.74 81.415 159.864
test5 3.881 5.075 6.904 14.123 50.258 129.937 166.162
test9 2.237 3.493 5.422 11.977 45.98 89.336 177.39
test6 1.261 2.12 4.38 10.698 31.821 86.106 186.636
test7 1.601 2.391 3.646 8.367 38.196 110.221 211.016
test1 1.529 2.381 3.527 8.411 40.551 105.16 212.573
test3 3.035 3.934 8.606 20.858 61.571 118.744 235.428
test2 3.136 6.238 10.508 33.48 43.532 118.044 239.481
test10 1.593 4.736 7.527 20.557 59.856 162.907 323.147
test11 3.913 11.506 23.26 68.644 207.591 600.444 1211.545
Apache Commons允许:
String myString = IOUtils.toString(myInputStream, "UTF-8");
当然,您可以选择除UTF-8之外的其他字符编码.
另见:( 文件)
考虑到文件一应该首先得到一个java.io.Reader
实例.然后可以将其读取并添加到StringBuilder
(StringBuffer
如果我们不在多个线程中访问它,我们就不需要了,并且StringBuilder
更快).这里的诀窍是我们在块中工作,因此不需要其他缓冲流.块大小参数化,用于运行时性能优化.
public static String slurp(final InputStream is, final int bufferSize) { final char[] buffer = new char[bufferSize]; final StringBuilder out = new StringBuilder(); try (Reader in = new InputStreamReader(is, "UTF-8")) { for (;;) { int rsz = in.read(buffer, 0, buffer.length); if (rsz < 0) break; out.append(buffer, 0, rsz); } } catch (UnsupportedEncodingException ex) { /* ... */ } catch (IOException ex) { /* ... */ } return out.toString(); }
使用:
InputStream in = /* Your InputStream */; StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String read; while ((read=br.readLine()) != null) { //System.out.println(read); sb.append(read); } br.close(); return sb.toString();
如果您使用的是Google-Collections/Guava,则可以执行以下操作:
InputStream stream = ... String content = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8)); Closeables.closeQuietly(stream);
请注意,第二个参数(即Charsets.UTF_8)InputStreamReader
不是必需的,但如果您知道它(通常应该!),通常最好指定编码.
这是我的纯Java和Android解决方案,它运行良好......
public String readFullyAsString(InputStream inputStream, String encoding) throws IOException { return readFully(inputStream).toString(encoding); } public byte[] readFullyAsBytes(InputStream inputStream) throws IOException { return readFully(inputStream).toByteArray(); } private ByteArrayOutputStream readFully(InputStream inputStream) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length = 0; while ((length = inputStream.read(buffer)) != -1) { baos.write(buffer, 0, length); } return baos; }
为了完整起见,这里是Java 9解决方案:
public static String toString(InputStream input) throws IOException { return new String(input.readAllBytes(), StandardCharsets.UTF_8); }
在readAllBytes
目前在JDK 9主要的基本代码,因此它可能出现在释放.您可以使用JDK 9快照构建立即尝试.
使用:
import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.IOException; public static String readInputStreamAsString(InputStream in) throws IOException { BufferedInputStream bis = new BufferedInputStream(in); ByteArrayOutputStream buf = new ByteArrayOutputStream(); int result = bis.read(); while(result != -1) { byte b = (byte)result; buf.write(b); result = bis.read(); } return buf.toString(); }
这是我在经过一些实验后想出的最优雅的纯Java(无库)解决方案:
public static String fromStream(InputStream in) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder out = new StringBuilder(); String newLine = System.getProperty("line.separator"); String line; while ((line = reader.readLine()) != null) { out.append(line); out.append(newLine); } return out.toString(); }
我在这里做了14个不同答案的基准(抱歉没有提供学分,但有太多重复).
结果非常令人惊讶.事实证明,Apache IOUtils是最慢的,ByteArrayOutputStream
也是最快的解决方案:
所以首先这里是最好的方法:
public String inputStreamToString(InputStream inputStream) throws IOException { try(ByteArrayOutputStream result = new ByteArrayOutputStream()) { byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } return result.toString(UTF_8); } }
时间以毫秒为单位
ByteArrayOutputStreamTest:194
NioStream:198
Java9ISTransferTo:201
Java9ISReadAllBytes:205
BufferedInputStreamVsByteArrayOutputStream:314
ApacheStringWriter2:574
GuavaCharStreams:589
ScannerReaderNoNextTest:614
ScannerReader:633
ApacheStringWriter:1544
StreamApi:错误
ParallelStreamApi:错误
BufferReaderTest:错误
InputStreamAndStringBuilder:错误
import com.google.common.io.CharStreams; import org.apache.commons.io.IOUtils; import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; import java.util.Arrays; import java.util.List; import java.util.Random; import java.util.stream.Collectors; /** * Created by Ilya Gazman on 2/13/18. */ public class InputStreamToString { private static final String UTF_8 = "UTF-8"; public static void main(String... args) { log("App started"); byte[] bytes = new byte[1024 * 1024]; new Random().nextBytes(bytes); log("Stream is ready\n"); try { test(bytes); } catch (IOException e) { e.printStackTrace(); } } private static void test(byte[] bytes) throws IOException { Listtests = Arrays.asList( new ApacheStringWriter(), new ApacheStringWriter2(), new NioStream(), new ScannerReader(), new ScannerReaderNoNextTest(), new GuavaCharStreams(), new StreamApi(), new ParallelStreamApi(), new ByteArrayOutputStreamTest(), new BufferReaderTest(), new BufferedInputStreamVsByteArrayOutputStream(), new InputStreamAndStringBuilder(), new Java9ISTransferTo(), new Java9ISReadAllBytes() ); String solution = new String(bytes, "UTF-8"); for (Stringify test : tests) { try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) { String s = test.inputStreamToString(inputStream); if (!s.equals(solution)) { log(test.name() + ": Error"); continue; } } long startTime = System.currentTimeMillis(); for (int i = 0; i < 20; i++) { try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) { test.inputStreamToString(inputStream); } } log(test.name() + ": " + (System.currentTimeMillis() - startTime)); } } private static void log(String message) { System.out.println(message); } interface Stringify { String inputStreamToString(InputStream inputStream) throws IOException; default String name() { return this.getClass().getSimpleName(); } } static class ApacheStringWriter implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, UTF_8); return writer.toString(); } } static class ApacheStringWriter2 implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { return IOUtils.toString(inputStream, UTF_8); } } static class NioStream implements Stringify { @Override public String inputStreamToString(InputStream in) throws IOException { ReadableByteChannel channel = Channels.newChannel(in); ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 16); ByteArrayOutputStream bout = new ByteArrayOutputStream(); WritableByteChannel outChannel = Channels.newChannel(bout); while (channel.read(byteBuffer) > 0 || byteBuffer.position() > 0) { byteBuffer.flip(); //make buffer ready for write outChannel.write(byteBuffer); byteBuffer.compact(); //make buffer ready for reading } channel.close(); outChannel.close(); return bout.toString(UTF_8); } } static class ScannerReader implements Stringify { @Override public String inputStreamToString(InputStream is) throws IOException { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; } } static class ScannerReaderNoNextTest implements Stringify { @Override public String inputStreamToString(InputStream is) throws IOException { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); return s.next(); } } static class GuavaCharStreams implements Stringify { @Override public String inputStreamToString(InputStream is) throws IOException { return CharStreams.toString(new InputStreamReader( is, UTF_8)); } } static class StreamApi implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { return new BufferedReader(new InputStreamReader(inputStream)) .lines().collect(Collectors.joining("\n")); } } static class ParallelStreamApi implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { return new BufferedReader(new InputStreamReader(inputStream)).lines() .parallel().collect(Collectors.joining("\n")); } } static class ByteArrayOutputStreamTest implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { try(ByteArrayOutputStream result = new ByteArrayOutputStream()) { byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } return result.toString(UTF_8); } } } static class BufferReaderTest implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { String newLine = System.getProperty("line.separator"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder result = new StringBuilder(UTF_8); String line; boolean flag = false; while ((line = reader.readLine()) != null) { result.append(flag ? newLine : "").append(line); flag = true; } return result.toString(); } } static class BufferedInputStreamVsByteArrayOutputStream implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { BufferedInputStream bis = new BufferedInputStream(inputStream); ByteArrayOutputStream buf = new ByteArrayOutputStream(); int result = bis.read(); while (result != -1) { buf.write((byte) result); result = bis.read(); } return buf.toString(UTF_8); } } static class InputStreamAndStringBuilder implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { int ch; StringBuilder sb = new StringBuilder(UTF_8); while ((ch = inputStream.read()) != -1) sb.append((char) ch); return sb.toString(); } } static class Java9ISTransferTo implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); inputStream.transferTo(bos); return bos.toString(UTF_8); } } static class Java9ISReadAllBytes implements Stringify { @Override public String inputStreamToString(InputStream inputStream) throws IOException { return new String(inputStream.readAllBytes(), UTF_8); } } }
我会使用一些Java 8技巧.
public static String streamToString(final InputStream inputStream) throws Exception { // buffering optional try ( final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)) ) { // parallel optional return br.lines().parallel().collect(Collectors.joining("\n")); } catch (final IOException e) { throw new RuntimeException(e); // whatever. } }
基本上与其他一些答案相同,除了更简洁.
我经常进行一些时间测试,因为时间很重要.
我试图以不同的方式将响应变为String 3.(如下所示)
为了便于阅读,我省略了try/catch块.
为了给出上下文,这是所有3种方法的前面的代码:
String response; String url = "www.blah.com/path?key=value"; GetMethod method = new GetMethod(url); int status = client.executeMethod(method);
1)
response = method.getResponseBodyAsString();
2)
InputStream resp = method.getResponseBodyAsStream(); InputStreamReader is=new InputStreamReader(resp); BufferedReader br=new BufferedReader(is); String read = null; StringBuffer sb = new StringBuffer(); while((read = br.readLine()) != null) { sb.append(read); } response = sb.toString();
3)
InputStream iStream = method.getResponseBodyAsStream(); StringWriter writer = new StringWriter(); IOUtils.copy(iStream, writer, "UTF-8"); response = writer.toString();
因此,在使用相同的请求/响应数据对每个方法运行500次测试之后,这里是数字.再一次,这些是我的发现,你的发现可能不完全相同,但我写这篇文章是为了向其他人说明这些方法的效率差异.
排名:
方法#1
方法#3 - 比#1
方法#2 慢2.6%- 比#1慢4.3%
这些方法中的任何一种都是用于获取响应并从中创建String的适当解决方案.
使用Stream的纯Java解决方案,自Java 8开始工作.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.stream.Collectors; // ... public static String inputStreamToString(InputStream is) throws IOException { try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) { return br.lines().collect(Collectors.joining(System.lineSeparator())); } }
正如ChristofferHammarström在其他答案中所提到的,明确指定Charset更安全.即InputStreamReader构造函数可以更改如下:
new InputStreamReader(is, Charset.forName("UTF-8"))
这里是或多或少的sampath的答案,清理了一下并表示为一个函数:
String streamToString(InputStream in) throws IOException { StringBuilder out = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); for(String line = br.readLine(); line != null; line = br.readLine()) out.append(line); br.close(); return out.toString(); }
如果您喜欢冒险,可以将Scala和Java混合使用,最后得到:
scala.io.Source.fromInputStream(is).mkString("")
混合Java和Scala代码和库有它的好处.
请在此处查看完整描述:在Scala中将InputStream转换为String的惯用方法
如果您不能使用Commons IO(FileUtils/IOUtils/CopyUtils),这是一个使用BufferedReader逐行读取文件的示例:
public class StringFromFile { public static void main(String[] args) /*throws UnsupportedEncodingException*/ { InputStream is = StringFromFile.class.getResourceAsStream("file.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(is/*, "UTF-8"*/)); final int CHARS_PER_PAGE = 5000; //counting spaces StringBuilder builder = new StringBuilder(CHARS_PER_PAGE); try { for(String line=br.readLine(); line!=null; line=br.readLine()) { builder.append(line); builder.append('\n'); } } catch (IOException ignore) { } String text = builder.toString(); System.out.println(text); } }
或者如果你想要原始速度,我会建议Paul de Vrieze建议的变体(这避免使用StringWriter(在内部使用StringBuffer):
public class StringFromFileFast { public static void main(String[] args) /*throws UnsupportedEncodingException*/ { InputStream is = StringFromFileFast.class.getResourceAsStream("file.txt"); InputStreamReader input = new InputStreamReader(is/*, "UTF-8"*/); final int CHARS_PER_PAGE = 5000; //counting spaces final char[] buffer = new char[CHARS_PER_PAGE]; StringBuilder output = new StringBuilder(CHARS_PER_PAGE); try { for(int read = input.read(buffer, 0, buffer.length); read != -1; read = input.read(buffer, 0, buffer.length)) { output.append(buffer, 0, read); } } catch (IOException ignore) { } String text = output.toString(); System.out.println(text); } }
如果使用Stream Readers,请务必在结尾处关闭流
private String readStream(InputStream iStream) throws IOException { //build a Stream Reader, it can read char by char InputStreamReader iStreamReader = new InputStreamReader(iStream); //build a buffered Reader, so that i can read whole line at once BufferedReader bReader = new BufferedReader(iStreamReader); String line = null; StringBuilder builder = new StringBuilder(); while((line = bReader.readLine()) != null) { //Read till end builder.append(line); builder.append("\n"); // append new line to preserve lines } bReader.close(); //close all opened stuff iStreamReader.close(); //iStream.close(); //EDIT: Let the creator of the stream close it! // some readers may auto close the inner stream return builder.toString(); }
编辑:在JDK 7+上,您可以使用try-with-resources构造.
/** * Reads the stream into a string * @param iStream the input stream * @return the string read from the stream * @throws IOException when an IO error occurs */ private String readStream(InputStream iStream) throws IOException { //Buffered reader allows us to read line by line try (BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream))){ StringBuilder builder = new StringBuilder(); String line; while((line = bReader.readLine()) != null) { //Read till end builder.append(line); builder.append("\n"); // append new line to preserve lines } return builder.toString(); } }
这是一个改编自org.apache.commons.io.IOUtils
源代码的答案,适用于那些想要实现apache但不想要整个库的人.
private static final int BUFFER_SIZE = 4 * 1024; public static String inputStreamToString(InputStream inputStream, String charsetName) throws IOException { StringBuilder builder = new StringBuilder(); InputStreamReader reader = new InputStreamReader(inputStream, charsetName); char[] buffer = new char[BUFFER_SIZE]; int length; while ((length = reader.read(buffer)) != -1) { builder.append(buffer, 0, length); } return builder.toString(); }
这个很好,因为:
Charset的手安全.
您可以控制读取缓冲区大小.
您可以设置构建器的长度,但可能不完全相同.
没有库依赖项.
适用于Java 7或更高版本.
这是为了什么?
public static String convertStreamToString(InputStream is) throws IOException { StringBuilder sb = new StringBuilder(2048); // Define a size if you have an idea of it. char[] read = new char[128]; // Your buffer size. try (InputStreamReader ir = new InputStreamReader(is, StandardCharsets.UTF_8)) { for (int i; -1 != (i = ir.read(read)); sb.append(read, 0, i)); } return sb.toString(); }
这是在不使用任何第三方库的情况下转换InputStream
为完整的方法String
.使用StringBuilder
单线程环境以其它方式使用StringBuffer
.
public static String getString( InputStream is) throws IOException { int ch; StringBuilder sb = new StringBuilder(); while((ch = is.read()) != -1) sb.append((char)ch); return sb.toString(); }
以下是使用字节数组缓冲区仅使用JDK的方法.这实际上是commons-io IOUtils.copy()
方法的工作原理.您可以替换byte[]
使用char[]
,如果你从一个拷贝Reader
,而不是一个InputStream
.
import java.io.ByteArrayOutputStream; import java.io.InputStream; ... InputStream is = .... ByteArrayOutputStream baos = new ByteArrayOutputStream(8192); byte[] buffer = new byte[8192]; int count = 0; try { while ((count = is.read(buffer)) != -1) { baos.write(buffer, 0, count); } } finally { try { is.close(); } catch (Exception ignore) { } } String charset = "UTF-8"; String inputStreamAsString = baos.toString(charset);
Kotlin用户只需:
println(InputStreamReader(is).readText())
而
readText()
是Kotlin标准库的内置扩展方法.
另一个,适用于所有Spring用户:
import java.nio.charset.StandardCharsets;
import org.springframework.util.FileCopyUtils;
public String convertStreamToString(InputStream is) throws IOException {
return new String(FileCopyUtils.copyToByteArray(is), StandardCharsets.UTF_8);
}
实用方法org.springframework.util.StreamUtils
与其中的实用方法类似FileCopyUtils
,但它们在完成时将流保持打开状态.
使用Java 9中支持的java.io.InputStream.transferTo(OutputStream)和采用charset名称的ByteArrayOutputStream.toString(String):
public static String gobble(InputStream in, String charsetName) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); in.transferTo(bos); return bos.toString(charsetName); }
JDK中最简单的方法是使用以下代码snipplet.
String convertToString(InputStream in){ String resource = new Scanner(in).useDelimiter("\\Z").next(); return resource; }
这是我的基于Java 8的解决方案,它使用新的Stream API从以下各项收集所有行InputStream
:
public static String toString(InputStream inputStream) { BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream)); return reader.lines().collect(Collectors.joining( System.getProperty("line.separator"))); }
就...而言reduce
,concat
它可以用Java 8表示为:
String fromFile = new BufferedReader(new InputStreamReader(inputStream)).lines().reduce(String::concat).get();
在Groovy中
inputStream.getText()