Created
May 8, 2020 18:05
-
-
Save varundwarkani/09638b14cd091fdd8b2a39fa4593a5b9 to your computer and use it in GitHub Desktop.
For creating a temporary backup of the database before we restore the database file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void backupDatabaseForRestore(Activity activity, Context context) { | |
File dbfile = activity.getDatabasePath(DATABASE_NAME); | |
File sdir = new File(getFilePath(context, 0), "backup"); | |
String sfpath = sdir.getPath() + File.separator + BACKUP_RESTORE_ROLLBACK_FILE_NAME; | |
if (!sdir.exists()) { | |
sdir.mkdirs(); | |
} | |
File savefile = new File(sfpath); | |
if (savefile.exists()) { | |
Log.d(LOGGER, "Backup Restore - File exists. Deleting it and then creating new file."); | |
savefile.delete(); | |
} | |
try { | |
if (savefile.createNewFile()) { | |
int buffersize = 8 * 1024; | |
byte[] buffer = new byte[buffersize]; | |
int bytes_read = buffersize; | |
OutputStream savedb = new FileOutputStream(sfpath); | |
InputStream indb = new FileInputStream(dbfile); | |
while ((bytes_read = indb.read(buffer, 0, buffersize)) > 0) { | |
savedb.write(buffer, 0, bytes_read); | |
} | |
savedb.flush(); | |
indb.close(); | |
savedb.close(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
Log.d(LOGGER, "ex for restore file: " + e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment