Question

I'd like to copy one file to another directory. These files are database files. I've got something like this but I got error like:

05-15 13:15:01.055: W/System.err(31247): java.io.FileNotFoundException: /mnt/sdcard/storeUGif/backup/gifs.db (No such file or directory)

My destination paths:

public static final String DATABASE_INPUT_BACKUP = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/storeUGif/" + SqliteHelper.DATABASE_NAME;

public static final String OUTPUT_BACKUP_DATABASE = Environment.getExternalStorageDirectory().getAbsolutePath() + "/storeUGif/backup/" + SqliteHelper.DATABASE_NAME;

This is my code:

private void copyDatabase() throws IOException {

        File outPut = new File(Tools.OUTPUT_BACKUP_DATABASE);
    File inPut = new File(Tools.DATABASE_INPUT_BACKUP);

        FileInputStream is = new FileInputStream(inPut);


        OutputStream os = new FileOutputStream(outPut);


        byte[] buffer = new byte[1024];


        while (is.read(buffer) > 0) {


            os.write(buffer);


        }
        os.flush();
        os.close();
        is.close();
}

I'd like to make copy of file from source folder to OUTPUT_BACKUP but I got this error. How to solve it?

Was it helpful?

Solution

I think its because the backup folder does not exist. Add this line of code here:

File outPut = new File(Tools.OUTPUT_BACKUP_DATABASE);
outPut.mkdirs();

This should create all folders needed, and if the folders already exist it won't hurt anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top