1. Overview
1.概述
In this quick tutorial, we’re going to show how to convert a File to an InputStream — first using plain Java and then Guava and the Apache Commons IO library.
在这个快速教程中,我们将展示如何将文件转换为InputStream –首先使用普通Java,然后是Guava和Apache Commons IO库。
This article is part of the Java – Back to Basics series here on Baeldung.
本文是Java – Back to Basics系列的一部分,在Baeldung这里。
2. Convert Using Java
2.使用Java进行转换
We can use the IO package of Java to convert a File to different InputStreams.
我们可以使用Java的IO包来将一个文件转换为不同的InputStreams。。
2.1. FileInputStream
2.1.FileInputStream
Let’s start with the first and simplest one — using a FileInputStream:
让我们从第一个也是最简单的一个开始–使用一个FileInputStream。
@Test
public void givenUsingPlainJava_whenConvertingFileToInputStream_thenCorrect()
throws IOException {
File initialFile = new File("src/main/resources/sample.txt");
InputStream targetStream = new FileInputStream(initialFile);
}
2.2. DataInputStream
2.2.DataInputStream
Let’s look at another method, where we can use DataInputStream to read binary or primitive data from a file:
让我们看看另一种方法,我们可以使用DataInputStream来从文件中读取二进制或原始数据。
@Test
public final void givenUsingPlainJava_whenConvertingFileToDataInputStream_thenCorrect()
throws IOException {
final File initialFile = new File("src/test/resources/sample.txt");
final InputStream targetStream =
new DataInputStream(new FileInputStream(initialFile));
}
2.3. SequenceInputStream
2.3.序列输入流
Finally, let’s also look at how to use SequenceInputStream to concatenate the input stream of two files to a single InputStream:
最后,我们也来看看如何使用SequenceInputStream将两个文件的输入流连接成一个InputStream。
@Test
public final void givenUsingPlainJava_whenConvertingFileToSequenceInputStream_thenCorrect()
throws IOException {
final File initialFile = new File("src/test/resources/sample.txt");
final File anotherFile = new File("src/test/resources/anothersample.txt");
final InputStream targetStream = new FileInputStream(initialFile);
final InputStream anotherTargetStream = new FileInputStream(anotherFile);
InputStream sequenceTargetStream =
new SequenceInputStream(targetStream, anotherTargetStream);
}
Note that we’re not closing the resulting stream in these examples for legibility.
请注意,在这些例子中,我们没有关闭产生的流,以保证可读性。
3. Convert Using Guava
3.使用Guava进行转换
Next, let’s see the Guava solution, using an intermediary ByteSource:
接下来,让我们看看Guava的解决方案,使用一个中间的ByteSource。
@Test
public void givenUsingGuava_whenConvertingFileToInputStream_thenCorrect()
throws IOException {
File initialFile = new File("src/main/resources/sample.txt");
InputStream targetStream = Files.asByteSource(initialFile).openStream();
}
4. Convert Using Commons IO
4.使用Commons IO进行转换
Finally, let’s look at a solution using Apache Commons IO:
最后,让我们看一下使用Apache Commons IO的解决方案。
@Test
public void givenUsingCommonsIO_whenConvertingFileToInputStream_thenCorrect()
throws IOException {
File initialFile = new File("src/main/resources/sample.txt");
InputStream targetStream = FileUtils.openInputStream(initialFile);
}
And there we have it. Three simple and clean solutions for opening a stream from a Java file.
就这样,我们拥有了它。从Java文件中打开一个流的三个简单而干净的解决方案。
5. Conclusion
5.结论
In this article, we explored various ways to convert a File to InputStream by using different libraries.
在这篇文章中,我们探讨了通过使用不同的库将文件转换为InputStream的各种方法。
The implementation of all these examples and code snippets can be found over on GitHub. This is a Maven-based project, so it should be easy to import and run as it is.
所有这些例子和代码片断的实现都可以在GitHub上找到over。这是一个基于Maven的项目,所以应该很容易导入并按原样运行。