Eros Live
          Find the Way
          posts - 15,comments - 0,trackbacks - 0

          在程序里備份恢復數(shù)據(jù)

          public static boolean backupDatabase() {
              File dbFile = new File(Environment.getDataDirectory() + "/data/" + PKG + "/databases/" + DB_NAME);
           
              File exportDir = new File(Environment.getExternalStorageDirectory(), "pocket-voa");
              
              if (!exportDir.exists()) {
                  exportDir.mkdirs();
              }
              
              File file = new File(exportDir, dbFile.getName());
           
              try {
                  file.createNewFile();
                  copyFile(dbFile, file);
                  return true;
              } catch (IOException e) {
                  Log.e(TAG, "[backupDatabase] error", e);
                  return false;
              }
          }
           
          public static boolean restoreDatabase() {
              File dbFile = new File(Environment.getDataDirectory() + "/data/" + PKG + "/databases/" + DatabaseHelper.DB_NAME);
           
              File exportDbFile = new File(Environment.getExternalStorageDirectory() + "/pocket-voa/" + DatabaseHelper.DB_NAME);
              
              if (!exportDbFile.exists())
                  return false;
           
              try {
                  dbFile.createNewFile();
                  copyFile(exportDbFile, dbFile);
                  return true;
              } catch (IOException e) {
                  Log.e(TAG, "[restoreDatabase] error", e);
                  return false;
              }
          }
           
          private static void copyFile(File src, File dst) throws IOException {
              FileChannel inChannel = new FileInputStream(src).getChannel();
              FileChannel outChannel = new FileOutputStream(dst).getChannel();
              try {
                  inChannel.transferTo(0, inChannel.size(), outChannel);
              } finally {
                  if (inChannel != null)
                      inChannel.close();
                  if (outChannel != null)
                      outChannel.close();
              }
          }

          參考

          posted on 2010-07-26 17:24 Eros 閱讀(343) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 申扎县| 巨鹿县| 黎平县| 门头沟区| 中宁县| 巧家县| 高碑店市| 思茅市| 体育| 鲁甸县| 囊谦县| 莒南县| 敦煌市| 遵化市| 鸡东县| 盖州市| 怀仁县| 平潭县| 安达市| 寿阳县| 长顺县| 扶沟县| 崇明县| 湟源县| 海南省| 洮南市| 法库县| 布拖县| 婺源县| 治多县| 汉源县| 托克托县| 永靖县| 三门峡市| 平罗县| 太保市| 靖宇县| 上饶市| 望城县| 岐山县| 修文县|