Java InputStream to File inputStreamToFile(InputStream stream, File file)

Here you can find the source of inputStreamToFile(InputStream stream, File file)

Description

input Stream To File

License

Creative Commons License

Declaration

public static void inputStreamToFile(InputStream stream, File file) throws IOException 

Method Source Code


//package com.java2s;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static void inputStreamToFile(InputStream stream, File file) throws IOException {
        FileOutputStream output = null;
        try {/*from  w  w w. j a va 2 s  .c  o m*/
            output = new FileOutputStream(file);
            copyStream(stream, output);
        } finally {
            if (output != null) {
                output.close();
            }
        }
    }

    /**
     * Copies the data from one input stream to an
     * output stream.
     *
     * @param stream A readable data stream
     * @param output A writable data stream to write the data.
     * @throws IOException An I/O error occurred.
     */
    public static void copyStream(InputStream stream, OutputStream output) throws IOException {
        byte[] buffer = new byte[8192];
        while (true) {
            int count = stream.read(buffer, 0, buffer.length);
            if (count < 0) {
                break;
            }
            output.write(buffer, 0, count);
        }
    }
}

Related

  1. inputStreamToFile(InputStream in, File file)
  2. inputStreamToFile(InputStream ins, File file)
  3. inputstreamtofile(InputStream ins, File file)
  4. inputStreamToFile(InputStream is)
  5. inputStreamToFile(InputStream is, File targetFile)
  6. writeStreamToFile(File target, InputStream in)
  7. writeStreamToFile(InputStream _actual, File _file)
  8. writeStreamToFile(InputStream in, OutputStream out)
  9. writeStreamToFile(InputStream in, String fileName)