备份和恢复SQLite数据库到SD卡备份、数据库、SQLite、SD

2023-09-11 20:34:11 作者:你说深爱却是深碍☠

如何备份我的数据库到我的应用程序自动SD卡? 而后来,我怎么恢复呢?

How can I backup my database to the sdcard automatically in my app? And afterward, how do I restore it?

推荐答案

下面是我的code:

    // Local database
    InputStream input = new FileInputStream(from);

    // create directory for backup
    File dir = new File(DB_BACKUP_PATH);
    dir.mkdir();

    // Path to the external backup
    OutputStream output = new FileOutputStream(to);

    // transfer bytes from the Input File to the Output File
    byte[] buffer = new byte[1024];
    int length;
    while ((length = input.read(buffer))>0) {
        output.write(buffer, 0, length);
    }

    output.flush();
    output.close();
    input.close();