write Bytes To File via InputStream - Android File Input Output

Android examples for File Input Output:InputStream

Description

write Bytes To File via InputStream

Demo Code


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

import android.util.Log;

public class Main {
  private static final String TAG = "";

  public static void writeBytesToFile(InputStream is, File file)
      throws IOException {
    FileOutputStream fos = null;//from www.  j  a  v  a2s .  c o  m
    try {
      byte[] data = new byte[2048];
      int nbread = 0;
      fos = new FileOutputStream(file);
      while ((nbread = is.read(data)) > -1) {
        fos.write(data, 0, nbread);
      }
    } catch (Exception ex) {
      Log.e(TAG, "Exception: " + ex);
    } finally {
      if (fos != null) {
        fos.close();
      }
    }
  }
}

Related Tutorials