backup Database File - Android Database

Android examples for Database:Database Backup

Description

backup Database File

Demo Code


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.os.Environment;

public class Main {

  public static void backupDatabase() throws IOException {
    // Open your local db as the input stream
    String inFileName = "/data/data/com/databases/TempDB.sqlite";
    File dbFile = new File(inFileName);
    FileInputStream fis = new FileInputStream(dbFile);

    String outFileName = Environment.getExternalStorageDirectory()
        + "/TempDB.sqlite";
    // Open the empty db as the output stream
    OutputStream output = new FileOutputStream(outFileName);
    // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;/*  ww  w. j  ava2 s.c  o m*/
    while ((length = fis.read(buffer)) > 0) {
      output.write(buffer, 0, length);
    }
    // Close the streams
    output.flush();
    output.close();
    fis.close();
  }
}

Related Tutorials