Java - InputStreamをファイルに書き込む

Java – InputStreamをファイルに書き込む

1. 概要

このクイックチュートリアルでは、write an InputStream to a Fileの方法を説明します。最初にプレーンJavaを使用し、次にGuavaを使用し、最後にApache CommonsIOライブラリを使用します。

この記事は、例としてここのthe “Java – Back to Basic” tutorialの一部です。

参考文献:

Java –リーダーへの入力ストリーム

Java、Guava、およびApache Commons IOライブラリを使用してInputStreamをリーダーに変換する方法。

Java –ファイルをInputStreamに変換

JavaファイルからInputStreamを開く方法-プレーンJava、Guava、およびApache Commons IOライブラリを使用します。

Java InputStream to Byte ArrayおよびByteBuffer

プレーンJava、グアバ、またはCommons IOを使用して、InputStreamをbyte []に​​変換する方法。

2. プレーンJavaを使用して変換する

the Java solutionから始めましょう:

@Test
public void whenConvertingToFile_thenCorrect()
  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");
    OutputStream outStream = new FileOutputStream(targetFile);
    outStream.write(buffer);
}

この例では、入力ストリームには、ディスク上のファイルやインメモリストリームなどの既知の事前定義データが含まれていることに注意してください。 このため、we don’t need to do any bounds checkingを使用すると、メモリが許せば、一度に読み取りと書き込みを行うことができます。

Java - Write an Input Stream to a File

 

入力ストリームがongoing stream of dataにリンクされている場合(たとえば、進行中の接続からのHTTP応答)、ストリーム全体を1回読み取ることはできません。 その場合、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);
}

最後に、Java8を使用して同じ操作を実行できるもう1つの簡単な方法を次に示します。

@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. グアバを使用して変換

次へ–より単純なGuavaベースのソリューションを見てみましょう。

@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. CommonsIOを使用して変換する

そして最後に-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);
}

これで、InputStreamをファイルに書き込む3つの簡単な方法がわかりました。