Save InputStream To Path - Android java.io

Android examples for java.io:InputStream

Description

Save InputStream To Path

Demo Code

import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main{

    /**/*from  w w w. j  a v a 2 s.  com*/
     * Stream to a file.
     */
    public static void streamToPath(InputStream is, File directory,
            String name) throws Exception {
        File file = new File(directory, name);
        streamToPath(is, file);
    }
    public static void streamToPath(InputStream is, File file)
            throws Exception {
        Log.i(TAG, "Streaming to path " + file.getPath());
        OutputStream os = null;
        os = new FileOutputStream(file);
        int count = -1;
        byte[] buffer = new byte[10 * 1024];
        while ((count = is.read(buffer)) != -1) {
            os.write(buffer, 0, count);
        }
        os.close();
    }

}

Related Tutorials