Android InputStream Read readToFile(InputStream in, File localFile)

Here you can find the source of readToFile(InputStream in, File localFile)

Description

Reads the whole InputStream content into a local file

License

Apache License

Parameter

Parameter Description
in The InputStream
localFile The local file

Exception

Parameter Description
IOException If reading or writing fails

Declaration

private static void readToFile(InputStream in, File localFile)
        throws IOException, FileNotFoundException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

public class Main {
    /**//from w  w w  . java  2s .c o m
     * Reads the whole InputStream content into a local file
     * 
     * @param in
     *          The InputStream
     *  @param localFile
     *        The local file
     * @throws IOException
     *             If reading or writing fails
     */
    private static void readToFile(InputStream in, File localFile)
            throws IOException, FileNotFoundException {
        OutputStream output = null;
        try {
            output = new FileOutputStream(localFile);
            // download the file
            in = new BufferedInputStream(in);
            byte data[] = new byte[1024];
            int count;
            while ((count = in.read(data)) != -1) {
                output.write(data, 0, count);
            }
            output.flush();
            output.close();
        } finally {
            if (output != null) {
                output.flush(); // close the local file in any case
                output.close();
            }
        }
    }
}

Related

  1. readInputStreamToString(InputStream input, String enCoding)
  2. writeToStream(InputStream in, OutputStream out, boolean closeOnExit)
  3. readStream(InputStream in)
  4. readStream(InputStream in)
  5. readStream(InputStream in)
  6. readAllAndClose(InputStream is)
  7. readAllBytes(InputStream in)
  8. readAllBytesToOutput(InputStream is, OutputStream output)
  9. readAllNoClose(InputStream is)