Java InputStream to OutputStream copyStreamToFileBinary(final InputStream stream, final File output)

Here you can find the source of copyStreamToFileBinary(final InputStream stream, final File output)

Description

Helper function for writing a binary stream to a file.

License

Open Source License

Parameter

Parameter Description
stream The stream to be consumed.
output The file to output to

Exception

Parameter Description
IOException an exception

Return

The output file

Declaration

public static File copyStreamToFileBinary(final InputStream stream, final File output) throws IOException 

Method Source Code

//package com.java2s;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    /**//  ww  w . j  a  v  a 2  s  .  c  om
     *    Helper function for writing a binary stream to a file. The stream and
     *    the file are both closed on completion.
     *
     *   @param stream The stream to be consumed.
     *   @param output The file to output to
     *   @return The output file
     *   @throws IOException
     */
    public static File copyStreamToFileBinary(final InputStream stream, final File output) throws IOException {
        final FileOutputStream out = new FileOutputStream(output);
        final byte buf[] = new byte[1024];
        int len = 0;
        while ((len = stream.read(buf)) > 0)
            out.write(buf, 0, len);
        out.close();
        stream.close();

        return output;
    }

    /**
     * Utility method for quickly create a {@link BufferedReader} for
     * a given file.
     * @param file The file
     * @return the corresponding reader
     * @throws IOException if an error occurs
     */
    public static BufferedReader read(final File file) throws IOException {
        return new BufferedReader(new InputStreamReader(new FileInputStream(file)));
    }
}

Related

  1. copyStreamToFile(InputStream input, String outputPath)
  2. CopyStreamToFile(InputStream inputStream, File outputFile)
  3. copyStreamToFile(InputStream inputStream, File outputFile)
  4. copyStreamToFile(InputStream is, File outputFile)
  5. copyStreamToFile(InputStream stream, String outputFilePath)
  6. copyStreamToStream(final InputStream input, final OutputStream output)
  7. copyStreamToStream(InputStream in, OutputStream out)
  8. inputStreamToOutputStream(final InputStream in, final OutputStream out)
  9. inputStreamToOutputStream(final InputStream in, final OutputStream out, int buffer)