ChatGPT解决这个技术问题 Extra ChatGPT

如何在 Java 中将 InputStream 读取/转换为字符串?

如果您有一个 java.io.InputStream 对象,您应该如何处理该对象并生成一个 String

假设我有一个包含文本数据的 InputStream,并且我想将其转换为 String,例如,我可以将其写入日志文件。

获取 InputStream 并将其转换为 String 的最简单方法是什么?

public String convertStreamToString(InputStream is) {
    // ???
}
请记住,您需要考虑输入流的编码。系统默认值不一定总是你想要的。t
这些答案大部分是在 Java 9 之前编写的,但现在您可以使用 .readAllBytes 从 InputStream 获取字节数组。因此,简单的“new String(inputStream.readAllBytes())”使用 String 的 byte[] 构造函数即可。

L
Luke Hutchison

总结其他答案我发现了 11 种主要方法(见下文)。我写了一些性能测试(见下面的结果):

将 InputStream 转换为字符串的方法:

使用 IOUtils.toString (Apache Utils) 字符串结果 = IOUtils.toString(inputStream, StandardCharsets.UTF_8);使用 CharStreams (Guava) String result = CharStreams.toString(new InputStreamReader( inputStream, Charsets.UTF_8));使用 Scanner (JDK) Scanner s = new Scanner(inputStream).useDelimiter("\\A");字符串结果 = s.hasNext() ? s.next() : "";使用流 API (Java 8)。警告:此解决方案将不同的换行符(如 \r\n)转换为 \n。字符串结果 = new BufferedReader(new InputStreamReader(inputStream)) .lines().collect(Collectors.joining("\n"));使用并行流 API (Java 8)。警告:此解决方案将不同的换行符(如 \r\n)转换为 \n。字符串结果 = new BufferedReader(new InputStreamReader(inputStream)) .lines().parallel().collect(Collectors.joining("\n"));使用 InputStreamReader 和 StringBuilder (JDK) int bufferSize = 1024;字符 [] 缓冲区 = 新字符 [缓冲区大小]; StringBuilder out = new StringBuilder(); Reader in = new InputStreamReader(stream, StandardCharsets.UTF_8); for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { out.append(buffer, 0, numRead); } 返回 out.toString();使用 StringWriter 和 IOUtils.copy (Apache Commons) StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, "UTF-8");返回 writer.toString();使用 ByteArrayOutputStream 和 inputStream.read (JDK) ByteArrayOutputStream 结果 = new ByteArrayOutputStream();字节[]缓冲区=新字节[1024]; for (int length; (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 结果 = new StringBuilder(); for (String line; (line = reader.readLine()) != null; ) { if (result.length() > 0) { result.append(newLine); } result.append(line); } 返回结果.toString();使用 BufferedInputStream 和 ByteArrayOutputStream (JDK) BufferedInputStream bis = new BufferedInputStream(inputStream); ByteArrayOutputStream buf = new ByteArrayOutputStream(); for (int result = bis.read(); result != -1; result = bis.read()) { buf.write((byte) result); } // StandardCharsets.UTF_8.name() > JDK 7 return buf.toString("UTF-8");使用 inputStream.read() 和 StringBuilder (JDK)。警告:此解决方案存在 Unicode 问题,例如俄语文本(仅适用于非 Unicode 文本) StringBuilder sb = new StringBuilder(); for (int ch; (ch = inputStream.read()) != -1; ) { sb.append((char) ch); } 返回 sb.toString();

警告:

解决方案 4、5 和 9 将不同的换行符转换为一个。解决方案 11 无法正确处理 Unicode 文本

性能测试

String(长度 = 175)、github 中的 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

big String (length = 50100), url in github 的性能测试(mode = Average Time, system = Linux, score 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

https://i.stack.imgur.com/AYYhz.png

性能测试(平均时间)取决于 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

不错的作品。在底部提供一个 tl;dr 总结可能很有用,即抛出存在换行/ unicode 问题的解决方案,然后(在剩下的那些中)说有或没有外部库最快的解决方案。
这个答案似乎不完整
我对自发布此答案以来添加的 Java 9 InputStream.transferTo 和 Java 10 Reader.transferTo 解决方案感到好奇,因此我查看了链接代码并为它们添加了基准。我只测试了“大字符串”基准。 InputStream.transferTo 是所有测试过的解决方案中最快的,运行时间是 test8 在我机器上的 60%。 Reader.transferTotest8 慢,但比所有其他测试都快。也就是说,它在 95% 的时间内以 test1 的形式运行,所以这并不是一个显着的改进。
我在对这篇文章的编辑中将所有 while 循环转换为 for 循环,以避免在循环外使用未使用的变量污染命名空间。这是一个适用于大多数 Java 读取器/写入器循环的巧妙技巧。
在 Java 9 中,您可以使用 .readAllBytes 从 InputStream 中获取字节数组。所以“new String(inputStream.readAllBytes())”使用 String 的 byte[] 构造函数工作。
M
Marko Zajc

一个很好的方法是使用 Apache commons IOUtilsInputStream 复制到 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); 

或者,如果您不想混合使用 Streams 和 Writer,您可以使用 ByteArrayOutputStream


toString 被弃用了吗?我看到IOUtils.convertStreamToString()
我添加了一个编辑以包含指向实际源代码本身的可搜索链接作为参考。我相信这增加了那些想要了解该命令如何工作的人的答案。
P
Peter Mortensen

这是一种仅使用标准 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,他曾向我指出上述文章。


我们不应该在返回值之前关闭扫描仪吗?
P
Peter Mortensen

Apache Commons 允许:

String myString = IOUtils.toString(myInputStream, "UTF-8");

当然,您可以选择除 UTF-8 之外的其他字符编码。

另请参阅:(documentation)


试图取回 InputStream,但不工作 stackoverflow.com/q/66349701/3425489
1
11 revs, 8 users 39%

考虑到文件 one 应该首先获得一个 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();
}

P
Peter Mortensen

利用:

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();

readLine() 删除换行符,因此生成的字符串将不包含换行符,除非您在添加到构建器的每一行之间添加换行符。
r
ralfoide

如果您使用的是 Google-Collections/Guava,您可以执行以下操作:

InputStream stream = ...
String content = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
Closeables.closeQuietly(stream);

请注意,InputStreamReader 的第二个参数(即 Charsets.UTF_8)不是必需的,但如果您知道编码,通常最好指定编码(您应该这样做!)


M
M. Justin

为了完整起见,这里是 Java 9 解决方案:

public static String toString(InputStream input) throws IOException {
    return new String(input.readAllBytes(), StandardCharsets.UTF_8);
}

这使用添加到 Java 9 的 readAllBytes 方法。


我对这个 here 进行了基准测试,发现这是我机器上最快的解决方案,运行时间大约是第二快的基准测试解决方案的 60%。
>此方法阻塞,直到所有剩余字节都被读取并检测到流结束,或者抛出异常。
T
TacB0sS

这是最适合 Android 和任何其他 JVM 的纯 Java 解决方案。

这个解决方案运行得非常好......它简单、快速,并且在大小流上都一样! (参见上面的基准。第 8 号)

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;
}

P
Peter Mortensen

利用:

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();
}

D
Drew Noakes

这是我经过一些实验后想出的最优雅的纯 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();
}

P
Peter Mortensen

我在这里对 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);
    }
}

20 个周期内 20 MB 随机字节的基准测试结果

以毫秒为单位的时间

字节数组输出流测试:194

NioStream:198

Java9ISTransferTo:201

Java9ISReadAllBytes:205

BufferedInputStreamVsByteArrayOutputStream:314

ApacheStringWriter2:574

GuavaCharStreams:589

ScannerReaderNoNextTest: 614

扫描仪阅读器: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 {
        List<Stringify> tests = 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);
        }
    }

}

Making benchmarks in Java is not easy(尤其是因为 JIT)。在阅读了 Benchmark 源代码后,我确信上面的那些值并不精确,每个人都应该小心地相信它们。
@Dalibor,您可能应该为您的主张提供更多理由,而不仅仅是一个链接。
我认为这是一个众所周知的事实,即制作自己的基准并不容易。对于那些不知道的人,有链接;)
@Dalibor 我可能不是最好的,但我对 Java 基准有很好的理解,所以除非你能指出一个具体的问题,否则你只是在误导,在这种情况下我不会继续与你交谈。
从公认的答案:规则 0:阅读论文,它基本上警告不要尝试微基准测试。规则 1:你没有热身阶段。规则 2-3:你没有表明你使用了这些标志。规则 8:使用 JMH 之类的库。评论中有 135 票:不要使用 System.currentTimeMillis()。继续其他高度投票的答案。 Jon Skeet:在迭代之间使用 System.gc(),并运行足够长的时间来测量结果,以秒为单位,而不是毫秒。在单个 JVM 运行中混合测试是不好的,因为为一个测试完成的编译器优化会影响另一个测试。
I
Ian2thedv

我会使用一些 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.
    }
}

除了更简洁之外,与其他一些答案基本相同。


m
martijnn2008

我进行了一些计时测试,因为时间总是很重要。

我试图以 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.6% 方法 #2 - 比 #1 慢 4.3%

这些方法中的任何一种都是获取响应并从中创建字符串的合适解决方案。


C
Community

使用 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()));
    }
}

正如 Christoffer Hammarström 在 other answer 下所提到的,明确指定 Charset 会更安全。即 InputStreamReader 构造函数可以更改如下:

new InputStreamReader(is, Charset.forName("UTF-8"))

T
TKH

这里或多或少是 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();
}

C
Community

如果您喜欢冒险,您可以混合使用 Scala 和 Java,然后得到以下结果:

scala.io.Source.fromInputStream(is).mkString("")

混合 Java 和 Scala 代码和库有它的好处。

在此处查看完整说明:Idiomatic way to convert an InputStream to a String in Scala


P
Peter Mortensen

如果您不能使用 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);
    }
}

j
jmehrens

使用 Java 9 中支持的 java.io.InputStream.transferTo(OutputStream) 和采用字符集名称的 ByteArrayOutputStream.toString(String)

public static String gobble(InputStream in, String charsetName) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    in.transferTo(bos);
    return bos.toString(charsetName);
}

T
Thamme Gowda

如果您使用流阅读器,请确保在最后关闭流

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();
    }
}

H
Hai Zhang

这是改编自 org.apache.commons.io.IOUtils source code 的答案,适用于那些想要 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();
}

D
Daniel De León

这个很好,因为:

它安全地处理字符集。

您可以控制读取缓冲区的大小。

您可以设置构建器的长度,它不必是精确值。

没有库依赖。

适用于 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();
}

对于 JDK 9

public static String inputStreamString(InputStream inputStream) throws IOException {
    try (inputStream) {
        return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
    }
}

c
czerny
String inputStreamToString(InputStream inputStream, Charset charset) throws IOException {
    try (
            final StringWriter writer = new StringWriter();
            final InputStreamReader reader = new InputStreamReader(inputStream, charset)
        ) {
        reader.transferTo(writer);
        return writer.toString();
    }
}

纯 Java 标准库解决方案 - 无库

从 Java 10 开始 - Reader#transferTo(java.io.Writer)

无环解决方案

没有换行符处理


r
rtruszk

这是在不使用任何第三方库的情况下将 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();
}

J
James

另一个,适用于所有 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 中的方法相似,但它们在完成后保持流打开。


M
Matt

以下是使用字节数组缓冲区仅使用 JDK 的方法。这实际上是 commons-io IOUtils.copy() 方法的全部工作方式。如果您从 Reader 而不是 InputStream 复制,则可以将 byte[] 替换为 char[]

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);

A
Alex

Kotlin 用户只需执行以下操作:

println(InputStreamReader(is).readText())

然而

readText()

是 Kotlin 标准库的内置扩展方法。


R
Raghu K Nair

JDK 中最简单的方法是使用以下代码片段。

String convertToString(InputStream in){
    String resource = new Scanner(in).useDelimiter("\\Z").next();
    return resource;
}

D
Derlin

reduceconcat 而言,它可以在 Java 8 中表示为:

String fromFile = new BufferedReader(new   
InputStreamReader(inputStream)).lines().reduce(String::concat).get();

C
Christian Rädel

这是我基于 Java 8 的解决方案,它使用 新的 Stream APIInputStream 收集所有行:

public static String toString(InputStream inputStream) {
    BufferedReader reader = new BufferedReader(
        new InputStreamReader(inputStream));
    return reader.lines().collect(Collectors.joining(
        System.getProperty("line.separator")));
}