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

          在程序里備份恢復數據

          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 閱讀(345) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 合作市| 平陆县| 界首市| 专栏| 左贡县| 祁阳县| 五原县| 临夏县| 睢宁县| 亚东县| 深州市| 化州市| 红原县| 泾川县| 鸡西市| 福建省| 隆昌县| 乌苏市| 米泉市| 夏河县| 龙陵县| 凤冈县| 钦州市| 慈溪市| 甘孜| 大渡口区| 昭苏县| 威信县| 北宁市| 图片| 宣恩县| 北辰区| 洛宁县| 温州市| 罗甸县| 南皮县| 上蔡县| 武功县| 武邑县| 瑞安市| 无棣县|