JavaのInputStreamをFileに変換する方法

JavaでInputStreamをファイルに変換する方法

以下は、InputStreamFileに変換するJavaの例です。

  1. InputStreamFileOutputStreamに手動でコピーします

  2. Apache Commons IO –FileUtils.copyInputStreamToFile

  3. Java 1.7 NIOFiles.copy

1. FileOutputStream

1.1 We have to copy the data from InputStream into OutputStream manually.

InputStreamToFile.java

package com.example;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

public class InputStreamToFile {

    private static final String FILE_TO = "d:\\download\\google.txt";

    public static void main(String[] args) throws IOException {

        URI u = URI.create("https://www.google.com/");
        try (InputStream inputStream = u.toURL().openStream()) {
            File file = new File(FILE_TO);
            copyInputStreamToFile(inputStream, file);
        }
    }

    // InputStream -> File
    private static void copyInputStreamToFile(InputStream inputStream, File file)
        throws IOException {

        try (FileOutputStream outputStream = new FileOutputStream(file)) {

            int read;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }

            // commons-io
            //IOUtils.copy(inputStream, outputStream);

        }

    }

}

2. Apache Commons IO

2.1 FileUtils.copyInputStreamToFile is available in the Apache Commons IO

pom.xml

    
        commons-io
        commons-io
        2.6
    

InputStreamToFile2.java

package com.example;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

public class InputStreamToFile2 {

    private static final String FILE_TO = "d:\\download\\google.txt";

    public static void main(String[] args) throws IOException {

        URI u = URI.create("https://www.google.com/");
        try (InputStream inputStream = u.toURL().openStream()) {

            File file = new File(FILE_TO);

            // commons-io
            FileUtils.copyInputStreamToFile(inputStream, file);
        }

    }

}

3. Java 1.7 NIO

3.1 If just want to save inputStream into a file somewhere, try the Java 1.7 NIO Files.copy

InputStreamToFile3.java

package com.example;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;

public class InputStreamToFile3 {

    private static final String FILE_TO = "d:\\download\\google.txt";

    public static void main(String[] args) throws IOException {

        URI u = URI.create("https://www.google.com/");
        try (InputStream inputStream = u.toURL().openStream()) {

            //Java 1.7
            Files.copy(inputStream, Paths.get(FILE_TO));

        }

    }

}

4. 昔な日々

4.1 In the old days, before Java 1.7, we have to close all resources manually.

InputStreamToFile4.java

package com.example;

import java.io.*;

public class InputStreamToFile4 {

    public static void main(String[] args) {

        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            // read this file into InputStream
            inputStream = new FileInputStream("/Users/example/holder.js");

            // write the inputStream to a FileOutputStream
            outputStream = new FileOutputStream(new File("/Users/example/holder-new.js"));

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }

            System.out.println("Done!");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

}

5. ファイルをInputStreamに変換する

とても簡単です:

    File file = new File("d:\\download\\google.txt");
    InputStream inputStream = new FileInputStream(file);

Note
このInputStream to Stringに興味があるかもしれません