Android——SQLite数据库备份与还原之外部存储

参考Android 数据库备份与还原
定义了一个BackupTask类,对文件进行读写操作:
⚪ 备份:将3个数据库文件复制至外部存储
⚪ 还原:从外部存储复制覆盖掉原数据库文件

public class BackupTask {
    private static final String COMMAND_BACKUP = "backupDatabase";
    private static final String COMMAND_RESTORE = "restoreDatabase";
    private final static String EXTERNAL_STORAGE_FOLDER = "Never Forget";
    private final static String EXTERNAL_STORAGE_BACKUP_DIR = "Backup";
    public String backup_version;
    @SuppressLint("StaticFieldLeak")
    private Context mContext;

    public BackupTask(Context context) {
        this.mContext = context;
    }

    private static File getExternalStoragePublicDir() {//    /sdcard/Never Forget/
        String path = Environment.getExternalStorageDirectory() + File.separator + EXTERNAL_STORAGE_FOLDER + File.separator;
        File dir = new File(path);
        if (!dir.exists()) dir.mkdirs();
        return dir;
    }

    public String doInBackground(String command) {
        File dbFile = mContext.getDatabasePath("event_database");// 默认路径是 /data/data/(包名)/databases/*
        File dbFile_shm = mContext.getDatabasePath("event_database-shm");// 默认路径是 /data/data/(包名)/databases/*
        File dbFile_wal = mContext.getDatabasePath("event_database-wal");// 默认路径是 /data/data/(包名)/databases/*
        File exportDir = new File(getExternalStoragePublicDir(), EXTERNAL_STORAGE_BACKUP_DIR);//    /sdcard/Never Forget/Backup
        if (!exportDir.exists()){
            exportDir.mkdirs();
        }
        File backup = new File(exportDir, dbFile.getName());//备份文件与原数据库文件名一致
        File backup_shm = new File(exportDir, dbFile_shm.getName());//备份文件与原数据库文件名一致
        File backup_wal = new File(exportDir, dbFile_wal.getName());//备份文件与原数据库文件名一致
        if (command.equals(COMMAND_BACKUP)) {
            try {
                backup.createNewFile();
                backup_shm.createNewFile();
                backup_wal.createNewFile();
                fileCopy(dbFile, backup);//数据库文件拷贝至备份文件
                fileCopy(dbFile_shm, backup_shm);//数据库文件拷贝至备份文件
                fileCopy(dbFile_wal, backup_wal);//数据库文件拷贝至备份文件
                backup_version = MyTimeUtils.getTimeString("yyyy.MM.dd_HH:mm:ss");
                //backup.setLastModified(MyTimeUtils.getTimeLong());
                Log.d("myLog", "backup ok! 备份文件名:"+backup.getName()+"\t"+backup_version);
                return backup_version;
            } catch (Exception e) {
                e.printStackTrace();
                Log.d("myLog", "backup fail! 备份文件名:"+backup.getName());
                return null;
            }
        } else if (command.equals(COMMAND_RESTORE)) {
            try {
                fileCopy(backup, dbFile);//备份文件拷贝至数据库文件
                fileCopy(backup_shm, dbFile_shm);//备份文件拷贝至数据库文件
                fileCopy(backup_wal, dbFile_wal);//备份文件拷贝至数据库文件
                backup_version = MyTimeUtils.getTimeString(backup.lastModified(),"yyyy.MM.dd_HH:mm:ss");
                Log.d("myLog", "restore success! 数据库文件名:"+dbFile.getName()+"\t"+backup_version);
                return backup_version;
            } catch (Exception e) {
                e.printStackTrace();
                Log.d("myLog", "restore fail! 数据库文件名:"+dbFile.getName());
                return null;
            }
        } else {
            return null;
        }
    }

    private void fileCopy(File dbFile, File backup) throws IOException {
        FileChannel inChannel = new FileInputStream(dbFile).getChannel();
        FileChannel outChannel = new FileOutputStream(backup).getChannel();
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inChannel != null) {
                inChannel.close();
            }
            if (outChannel != null) {
                outChannel.close();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值