章
目
录
在Java中,将InputStream转换为String是一个非常常见的需求,特别是在需要从网络流或文件系统读取文件的应用程序中。下面提供了三种方法来实现这一转换。
1.使用InputStream.readAllBytes()(自Java 9起)
InputStream.readAllBytes() API将输入流转换为字节。然后,我们使用new String()创建一个新的String对象。
InputStream in = new FileInputStream(new File("C:/temp/test.txt"));
String fileContent = new String( in.readAllBytes() );
这在内部使用readNBytes(Integer.MAX_VALUE)方法,这意味着它不应用于读取大于2GB的文件大小。
2.使用BufferedReader
使用BufferedReader是最简单和最流行的方法,用于将文件读取为InputStream并按行处理。
// 使用BufferedReader
InputStream in = new FileInputStream(new File("C:/temp/test.txt"));
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
}
String fileContent = out.toString();
reader.close();
另外,我们可以使用BufferedReader.lines()方法(自Java 8起)来获取行的Stream并按需处理内容。
InputStream in = new FileInputStream(new File("C:/temp/test.txt"));
String newLine = System.getProperty("line.separator");
String fileContent;
try (Stream<String> lines = new BufferedReader(new InputStreamReader(in)).lines()) {
fileContent = lines.collect(Collectors.joining(newLine));
}
//Use fileContent
3.Google Guava IO
Guava库包含一些非常有用的类和方法来执行IO操作。这些类隐藏了所有复杂性,否则会暴露出来。
3.1 依赖项
Maven依赖项用于Google Guava。
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
3.2 使用ByteSource
ByteSource表示可读的字节源,例如文件。它具有实用工具方法,通常通过打开流,执行某些操作,并最终关闭打开的流来实现。
其asCharSource(charset)方法将读取的字节解码为给定Charset中的字符。它返回作为方法的输出字符的字符串。
InputStream inputStream = new FileInputStream(new File("C:/temp/test.txt"));
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return inputStream;
}
};
String fileContent = byteSource
.asCharSource(Charsets.UTF_8)
.read();
3.3 使用CharStreams
CharStreams类还提供了一些实用程序方法来处理字符流。使用InputStreamReader和CharStreams可以帮助将InputStream转换为String。
Java程序使用Google Guava库中的CharStreams类将InputStream转换为String。
InputStream inputStream = new FileInputStream(new File("C:/temp/test.txt"));
String fileContent = null;
try (final Reader reader = new InputStreamReader(inputStream)) {
fileContent = CharStreams.toString(reader);
}
System.out.println(fileContent);
4.Apache Commons IO
4.1 依赖项
将以下依赖项包含在项目中以包含common-io jars。
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
4.2使用IOUtils
Apache commons有一个非常有用的类IOUtils来将文件内容读取到String中。它使代码更干净,易于阅读,并提供更好的性能。使用以下两种方法之一:
- IOUtils.copy()
- IOUtils.toString()
//方法1 IOUtils.copy()
StringWriter writer = new StringWriter();
IOUtils.copy(new FileInputStream(new File("C:/temp/test.txt")), writer, "UTF-8");
String fileContent = writer.toString();
System.out.println(fileContent);
//方法2 IOUtils.toString()
String fileContent = IOUtils.toString(new FileInputStream(new File("C:/temp/test.txt")), "UTF-8");
System.out.println(fileContent);
5.使用Scanner将Java InputStream转换为String
使用Scanner类并不流行,但它有效。原因是Scanner迭代流中的标记,在此过程中,我们可以使用“输入边界的开头”来分隔标记,从而为整个流的内容只给我们一个标记。
FileInputStream fin = new FileInputStream(new File("C:/temp/test.txt"));
java.util.Scanner scanner = new java.util.Scanner(fin,"UTF-8").useDelimiter("\A");
String fileContent = scanner.hasNext() ? scanner.next() : "";
scanner.close();
就是这样。这篇文章的目的是提供一个非常特定用途的快速链接,即读取InputStream到String。