1

I have found a tutorial that copies the database to a folder as backup and copies it back to the database folder of the app as a restore. Almost everything works fine except for one thing: when I delete the backed up .db file from my folder and click Restore in my app it looks for the file, cannot find it, then it overwrites the whole database with nothing. It's like I emptied the whole database.

Backup:

try {

                    InputStream myInput;
                    OutputStream myOutput;

                    myInput = new FileInputStream("/data/data/com.me.myapp/databases/HotOrNotdbLists2");//this is
        // the path for all apps
        //insert your package instead packagename,ex:com.mybusiness.myapp


                    // Set the output folder on the SDcard
                     File directory = new File("/sdcard/MyApp/");
                    // Create the folder if it doesn't exist:
                    if (!directory.exists()) 
                    {
                        directory.mkdirs();
                    } 
                    // Set the output file stream up:

                    myOutput = new FileOutputStream(directory.getPath()+
         "/myapp.db");

                    // Transfer bytes from the input file to the output file
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = myInput.read(buffer))>0)
                    {
                        myOutput.write(buffer, 0, length);
                    }
                    // Close and clear the streams

                    myOutput.flush();

                    myOutput.close();

                    myInput.close();
                    Toast.makeText(Settings.this, "Backup done successfully!", Toast.LENGTH_LONG).show();

                } catch (FileNotFoundException e) {
            Toast.makeText(Settings.this, "Backup unsuccessful! File cannot be created! Directory does not exist?", Toast.LENGTH_LONG).show();


                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
            Toast.makeText(Settings.this, "Backup unsuccessful!", Toast.LENGTH_LONG).show();


                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

Restore:

OutputStream myOutput;

                try {

                    myOutput = new FileOutputStream("/data/data/com.me.myapp/databases/HotOrNotdbLists2");


                    // Set the folder on the SDcard
                     File directory = new File("/sdcard/MyApp/");
                    // Set the input file stream up:

                    InputStream myInputs = new FileInputStream(directory.getPath()+ "/myapp.db");


                    // Transfer bytes from the input file to the output file
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = myInputs.read(buffer))>0)
                    {
                        myOutput.write(buffer, 0, length);
                    }


                    // Close and clear the streams
                    myOutput.flush();

                    myOutput.close();

                    myInputs.close();   
                  Toast.makeText(Settings.this, "Restore done successfully!", Toast.LENGTH_SHORT).show();

                } catch (FileNotFoundException e) {
        Toast.makeText(Settings.this, "Restore unsuccessful! File not found! Directory does not exist?", Toast.LENGTH_LONG).show();


                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {   Toast.makeText(Settings.this, "Restore unsuccessful!", 
        Toast.LENGTH_SHORT).show();


                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

Edit: I added Jason's answer and it still clears the database, even if I get false to directoryB and get the Restore unsuccessful! File not found! Directory/file does not exist? message ???!?!

try {
                        OutputStream myOutput;   
                        myOutput = new FileOutputStream("/data/data/com.me.myapp/databases/HotOrNotdbLists2");

                        // Set the folder on the SDcard
                        File directory = new File("/sdcard/MyApp/");
                        boolean directoryB = new File("/sdcard/MyApp/", "/myapp.db").exists();
   Toast.makeText(Settings.this, "directoryB: " + directoryB, Toast.LENGTH_SHORT).show();

                        if (directoryB == true)
                        {
                           Toast.makeText(Settings.this, "File exists!", Toast.LENGTH_SHORT).show();

                        // Set the input file stream up:

                        InputStream myInputs = new FileInputStream(directory.getPath()+ "/myapp.db");


                        // Transfer bytes from the input file to the output file
                        byte[] buffer = new byte[1024];
                        int length;
                        while ((length = myInputs.read(buffer))>0)
                        {
                            myOutput.write(buffer, 0, length);
                        }


                        // Close and clear the streams
                        myOutput.flush();

                        myOutput.close();

                        myInputs.close();   
                        Toast.makeText(Settings.this, "Restore done successfully!", Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                            Toast.makeText(Settings.this, "Restore unsuccessful! File not found! Directory/file does not exist?", Toast.LENGTH_SHORT).show();
                        }

                    } catch (FileNotFoundException e) {
            //Toast.makeText(Settings.this, "Restore unsuccessful! File not found! Directory does not exist?", Toast.LENGTH_LONG).show();


                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {   Toast.makeText(Settings.this, "Restore unsuccessful!", 
            Toast.LENGTH_SHORT).show();


                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

1 Answer 1

1

Check if the file exists first before continuing the restore operation, like so:

new File(directory, "/myapp.db").exists()

FileInputStream will create files automatically if they do not exist.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried, it still overwrites the app. I think the condition is okay, any ideas? I updated the main post
Move your check to the top of the method, so you don't execute anything if the file doesn't exist. Throw in some logs too to see if the file exists or not (may be the case the file isn't actually deleted).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.