title: how to restore backup file in room database easily?
body: <p>I can't Find a good Solution For this. Even I Don't Know Anything About taking BackUp And Restore BackUp File in Room DataBase. Please Help Me How to Do it.</p>
  <p>I Don&quot;t Have Any Longer Question But Stack OverFlow Giving me Error That's why I wrote this Line</p>
  <p>I found Way to take Back up But I Can't found the solution for restore database
  when I check app shows restore success but in real data not available in app</p>
  <pre><code> public static boolean backUpDatabase(Context context) throws IOException {
          db.close();
          File dbFile = new File(String.valueOf(context.getDatabasePath(MainDatabase.DATABASE_NAME)));
          File sDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),&quot;Tuition App2&quot;);
          String fileName = &quot;TuitionApp_BackUp.db&quot;;
          String sfPath = sDir.getAbsolutePath()+&quot;/&quot;+fileName;
          if (!sDir.exists()){
              sDir.mkdirs();
          }
          File saveFile = new File(sfPath);
          if (saveFile.exists()){
              saveFile.delete();
          }
          if (saveFile.createNewFile()){
              int bufferSize = 8 * 1024;
              byte[] buffer = new byte[bufferSize];
              int bytes_read = bufferSize;
              OutputStream saveDb = new FileOutputStream(sfPath);
              InputStream inDb = new FileInputStream(dbFile);
              while ((bytes_read = inDb.read(buffer, 0, bufferSize)) &gt; 0) {
                  saveDb.write(buffer, 0, bytes_read);
              }
              saveDb.flush();
              inDb.close();
              saveDb.close();
  //saving path in to sharedPraf  sm is object of that class
              sm.saveBackFileName(context,sfPath);
              return true;
          }
          return false;
      }
      public static boolean restoreDataBase(Context context) throws FileNotFoundException {
          db.close();
          File sDir = new File(sm.getBackupFileName(context));
          File finalFile = new File(sDir.getAbsolutePath());
          InputStream inputStream = context.getContentResolver().openInputStream(Uri.fromFile(finalFile));
          File oldDb = context.getDatabasePath(MainDatabase.DATABASE_NAME);
          if ( inputStream!= null){
              try {
                  copyFile((FileInputStream) inputStream,new FileOutputStream(oldDb));
                  return true;
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
  
          return false;
      }
      public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
          FileChannel fromChannel = null;
          FileChannel toChannel = null;
          try {
              fromChannel = fromFile.getChannel();
              toChannel = toFile.getChannel();
              fromChannel.transferTo(0, fromChannel.size(), toChannel);
  
          } finally {
              try {
                  if (fromChannel != null) {
                      fromChannel.close();
                  }
              } finally {
                  if (toChannel != null) {
                      toChannel.close();
                  }
              }
          }
      }
  
  </code></pre>