copy Streams to FileOutputStream - Android File Input Output

Android examples for File Input Output:Copy File

Description

copy Streams to FileOutputStream

Demo Code

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

import android.util.Log;

public class Main {
  static final String LOGTAG = "";
  static final int BUFSIZE = 5192;

  static void copyStreams(InputStream is, FileOutputStream fos) {
    BufferedOutputStream os = null;

    try {//  ww w  .  j av  a  2  s .  c o  m
      byte data[] = new byte[BUFSIZE];
      int count;
      os = new BufferedOutputStream(fos, BUFSIZE);

      while ((count = is.read(data, 0, BUFSIZE)) != -1) {
        os.write(data, 0, count);
      }

      os.flush();

    } catch (IOException e) {
      Log.e(LOGTAG, "Exception while copying: " + e);

    } finally {

      try {

        if (os != null) {
          os.close();
        }

      } catch (IOException e2) {
        Log.e(LOGTAG, "Exception while closing the stream: " + e2);
      }
    }
  }
}

Related Tutorials