Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7882675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:20:25+00:00 2026-06-03T04:20:25+00:00

I have found a tutorial that copies the database to a folder as backup

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:20:26+00:00Added an answer on June 3, 2026 at 4:20 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just found this great tutorial as it is something that I need.
I am working on a project that needs a database. I have found a
Every time I try to compile and run a tutorial that I have found,
I have found this great tutorial, about uploading files with a Flex app, using
I'd like some kind of overview tutorial that shows how this works. I have
I have found this tutorial , which looks great, except the fact, that I
I am relatively new to assembly language. I have found so many tutorials that
I have found a Tutorial here on how to implement drag and drop in
Can anyone please help. I'm following a tutorial found here as I have a
I have found myself designing a language for fun that is a cross between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.