Java中InputStream转换为String的方法

Java中InputStream转换为String的方法

技术背景

在Java编程中,经常会遇到需要将InputStream对象中的数据转换为String类型的需求。例如,当我们从网络获取数据、读取文件或者处理其他输入流时,为了方便后续处理,可能需要将输入流中的文本数据转换为字符串。本文将介绍多种将InputStream转换为String的方法,并对它们进行性能分析。

实现步骤

1. 使用Apache Commons IOUtils

1
2
3
4
5
6
7
8
9
10
import org.apache.commons.io.IOUtils;
import java.nio.charset.StandardCharsets;
import java.io.InputStream;
import java.io.IOException;

public class InputStreamToStringExample {
public static String convertUsingIOUtils(InputStream inputStream) throws IOException {
return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
}
}

2. 使用Guava CharStreams

1
2
3
4
5
6
7
8
9
10
11
import com.google.common.io.CharStreams;
import com.google.common.base.Charsets;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;

public class InputStreamToStringExample {
public static String convertUsingGuava(InputStream inputStream) throws IOException {
return CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
}
}

3. 使用Scanner

1
2
3
4
5
6
7
8
9
import java.util.Scanner;
import java.io.InputStream;

public class InputStreamToStringExample {
public static String convertUsingScanner(InputStream inputStream) {
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
}

4. 使用Stream API(Java 8)

1
2
3
4
5
6
7
8
9
10
11
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;

public class InputStreamToStringExample {
public static String convertUsingStreamAPI(InputStream inputStream) {
return new BufferedReader(new InputStreamReader(inputStream))
.lines().collect(Collectors.joining("\n"));
}
}

5. 使用InputStreamReader和StringBuilder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.io.IOException;

public class InputStreamToStringExample {
public static String convertUsingInputStreamReader(InputStream inputStream) throws IOException {
int bufferSize = 1024;
char[] buffer = new char[bufferSize];
StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {
out.append(buffer, 0, numRead);
}
return out.toString();
}
}

6. 使用ByteArrayOutputStream和inputStream.read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;

public class InputStreamToStringExample {
public static String convertUsingByteArrayOutputStream(InputStream inputStream) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int length; (length = inputStream.read(buffer)) != -1; ) {
result.write(buffer, 0, length);
}
return result.toString("UTF-8");
}
}

核心代码

以下是一个综合示例,展示了如何调用上述方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import org.apache.commons.io.IOUtils;
import com.google.common.io.CharStreams;
import com.google.common.base.Charsets;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.IOException;
import java.util.Scanner;
import java.io.BufferedReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

public class InputStreamToStringExample {
public static void main(String[] args) {
try {
InputStream inputStream = System.in;

// 使用Apache Commons IOUtils
String result1 = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
System.out.println("Using Apache Commons IOUtils: " + result1);

// 使用Guava CharStreams
String result2 = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
System.out.println("Using Guava CharStreams: " + result2);

// 使用Scanner
String result3 = new Scanner(inputStream).useDelimiter("\\A").next();
System.out.println("Using Scanner: " + result3);

// 使用Stream API(Java 8)
String result4 = new BufferedReader(new InputStreamReader(inputStream))
.lines().collect(Collectors.joining("\n"));
System.out.println("Using Stream API: " + result4);

// 使用InputStreamReader和StringBuilder
int bufferSize = 1024;
char[] buffer = new char[bufferSize];
StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {
out.append(buffer, 0, numRead);
}
String result5 = out.toString();
System.out.println("Using InputStreamReader and StringBuilder: " + result5);

// 使用ByteArrayOutputStream和inputStream.read
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
for (int length; (length = inputStream.read(buffer2)) != -1; ) {
result.write(buffer2, 0, length);
}
String result6 = result.toString("UTF-8");
System.out.println("Using ByteArrayOutputStream and inputStream.read: " + result6);

} catch (IOException e) {
e.printStackTrace();
}
}
}

最佳实践

  • 选择合适的方法:根据具体的场景和需求选择合适的转换方法。如果项目中已经引入了Apache Commons或Guava库,可以优先考虑使用它们提供的工具类,代码更简洁。如果需要兼容低版本的Java,避免使用Java 8及以上版本的特性。
  • 处理编码问题:在转换过程中,要注意输入流的编码格式,确保使用正确的字符集进行转换,避免出现乱码问题。
  • 资源管理:在使用InputStreamReader等资源时,要确保及时关闭,避免资源泄漏。可以使用try-with-resources语句来自动管理资源。

常见问题

  • 编码问题:如果输入流的编码格式与转换时使用的字符集不一致,会导致输出的字符串出现乱码。解决方法是明确输入流的编码格式,并在转换时使用正确的字符集。
  • 性能问题:不同的转换方法在性能上可能存在差异。例如,使用InputStream.read()逐个字符读取的方法性能较低,而使用ByteArrayOutputStream批量读取的方法性能较高。可以根据实际情况选择性能最优的方法。
  • 内存问题:如果输入流的数据量非常大,一次性将其转换为字符串可能会导致内存溢出。可以考虑分块处理输入流,避免一次性加载过多数据到内存中。

Java中InputStream转换为String的方法
https://119291.xyz/posts/2025-04-17.java-inputstream-to-string-conversion/
作者
ww
发布于
2025年4月17日
许可协议