Java

您所在的位置:网站首页 输入流转为文件 Java

Java

2024-07-11 11:49| 来源: 网络整理| 查看: 265

1. Overview

In this quick tutorial, we’ll illustrate how to write an InputStream to a File. First we’ll use plain Java, then Guava, and finally the Apache Commons IO library.

This article is part of the “Java – Back to Basic” tutorial here on Baeldung.

2. Convert Using Plain Java

Let’s start with the Java solution:

@Test public void whenConvertingToFile_thenCorrect() throws IOException { Path path = Paths.get("src/test/resources/sample.txt"); byte[] buffer = java.nio.file.Files.readAllBytes(path); File targetFile = new File("src/test/resources/targetFile.tmp"); OutputStream outStream = new FileOutputStream(targetFile); outStream.write(buffer); IOUtils.closeQuietly(outStream); }

Note that in this example, the input stream has known and pre-determined data, such as a file on disk or an in-memory stream. As a result, we don’t need to do any bounds checking and we can, if memory allows, simply read it and write it in one go.

If the input stream is linked to an ongoing stream of data, like an HTTP response coming from an ongoing connection, then reading the entire stream once isn’t an option. In that case, we need to make sure we keep reading until we reach the end of the stream:

@Test public void whenConvertingInProgressToFile_thenCorrect() throws IOException { InputStream initialStream = new FileInputStream( new File("src/main/resources/sample.txt")); File targetFile = new File("src/main/resources/targetFile.tmp"); OutputStream outStream = new FileOutputStream(targetFile); byte[] buffer = new byte[8 * 1024]; int bytesRead; while ((bytesRead = initialStream.read(buffer)) != -1) { outStream.write(buffer, 0, bytesRead); } IOUtils.closeQuietly(initialStream); IOUtils.closeQuietly(outStream); }

Finally, here’s another simple way we can use Java 8 to do the same operation:

@Test public void whenConvertingAnInProgressInputStreamToFile_thenCorrect2() throws IOException { InputStream initialStream = new FileInputStream( new File("src/main/resources/sample.txt")); File targetFile = new File("src/main/resources/targetFile.tmp"); java.nio.file.Files.copy( initialStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); IOUtils.closeQuietly(initialStream); } 3. Convert Using Guava

Next, let’s take a look at a simpler Guava based solution:

@Test public void whenConvertingInputStreamToFile_thenCorrect3() throws IOException { InputStream initialStream = new FileInputStream( new File("src/main/resources/sample.txt")); byte[] buffer = new byte[initialStream.available()]; initialStream.read(buffer); File targetFile = new File("src/main/resources/targetFile.tmp"); Files.write(buffer, targetFile); } 4. Convert Using Commons IO

Finally, here’s an even quicker solution with Apache Commons IO:

@Test public void whenConvertingInputStreamToFile_thenCorrect4() throws IOException { InputStream initialStream = FileUtils.openInputStream (new File("src/main/resources/sample.txt")); File targetFile = new File("src/main/resources/targetFile.tmp"); FileUtils.copyInputStreamToFile(initialStream, targetFile); }

And there we have it, 3 quick ways of writing the InputStream to a File.

The implementation of all these examples can be found in our GitHub project.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3