代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
数据库备份和恢复
作者:
523450269
/ 发布于
2013/7/19
/
803
package cn.fly.backup; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; import java.text.SimpleDateFormat; import java.util.Date; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.AsyncTask; import android.os.Environment; import android.view.ViewGroup.LayoutParams; import android.view.Window; import android.view.WindowManager; import android.widget.ProgressBar; import android.widget.Toast; public class BackupAndRestore { private Context mContext = null; private String[] fileList = null; // 数据库文件列表 private int choicePostion = -3; // 选择数据库列表中的位置 private AlertDialog dialog = null; private String BACK_FOLDER = "backup"; private String appName = "myApp"; public BackupAndRestore(Context context) { mContext = context; } /** * 恢复数据的Dialog */ public void restoreDB() { fileList = getFileList(); AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setIcon(android.R.drawable.ic_dialog_info); builder.setTitle("恢复"); builder.setSingleChoiceItems(getFileList(), -1, new DialogClick()); builder.setPositiveButton("确定", new DialogClick()); builder.setNegativeButton("取消", new DialogClick()); builder.show(); } /** * 备份数据库 */ public void backupDB() { showDialog("是否备份数据库", 'B'); } /** * 显示一个Dialog * * @param title * 标题 ,必须引用资源ID resource ID * @param sign * 根据标示调用方法 I - 恢复默认设置 D - 恢复默认设置 H -选择主机 */ private void showDialog(String title, char sign) { final char s = sign; new AlertDialog.Builder(mContext).setTitle(title) .setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialogI, int which) { switch (s) { case 'B': // 备份数据库 if (dialog == null) { dialog = awaitDialog(mContext); } else { dialog.show(); } new ExecuteTask().execute('B'); break; default: break; } } }).setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); } /** * 备份操作 * * @return */ private boolean backUp() { boolean isOk = false; String sp = File.separator; File sdFile = sdCardOk(); if (sdFile != null) { try { String[] dbNames = { "数据库名称" }; // 创建日期文件夹 String folder_date = datePrefix(); File f = new File(sdFile.getAbsolutePath() + sp + folder_date); if (!f.exists()) { f.mkdirs(); } for (int i = 0; i < dbNames.length; i++) { String dbName = dbNames[i]; File dbFile = dbOk(dbName); if (dbFile != null) { File backFile = new File(f.getAbsolutePath() + sp + dbFile.getName()); backFile.createNewFile(); isOk = fileCopy(backFile, dbFile.getAbsoluteFile()); if (!isOk) { break; } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return isOk; } /** * 时间前缀 * * @return */ private String datePrefix() { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); Date date = new Date(System.currentTimeMillis()); String str = format.format(date); return str; } /** * 文件夹列表 * * @return */ private String[] getFileList() { String[] fileList = null; File file = sdCardOk(); if (file != null) { File[] list = file.listFiles(); if (list != null && list.length > 0) { fileList = new String[list.length]; for (int i = 0; i < list.length; i++) { fileList[i] = list[i].getName(); } } } return fileList; } /** * sdCard是否存在 备份的文件夹是否存在 * * @return null不能使用 */ private File sdCardOk() { File bkFile = null; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { String sp = File.separator; String backUpPath = Environment.getExternalStorageDirectory() + sp + appName + sp + BACK_FOLDER; bkFile = new File(backUpPath); if (!bkFile.exists()) { bkFile.mkdirs(); } else return bkFile; } else Toast.makeText(mContext, "Sdcard 不存在", Toast.LENGTH_SHORT).show(); return bkFile; } /** * 恢复数据库 * * @param name * 选择的文件名称 选中的数据库名称 * @param resoreDbName * 需要恢复的数据库名称 * @return */ public boolean restore(String name, File f) { boolean isOk = false; if (f != null) { File dbFile = dbOk(name); try { // System.out.println("覆盖的名称"+dbName); if (dbFile != null) { isOk = fileCopy(dbFile, f.getAbsoluteFile()); } else isOk = false; } catch (IOException e) { e.printStackTrace(); } } return isOk; } /** * 数据库文件是否存在,并可以使用 * * @return */ private File dbOk(String dbName) { String sp = File.separator; String absPath = Environment.getDataDirectory().getAbsolutePath(); String pakName = mContext.getPackageName(); String dbPath = absPath + sp + "data" + sp + pakName + sp + "databases" + sp + dbName; File file = new File(dbPath); if (file.exists()) { return file; } else { return null; } } /** * 等候动画 */ public AlertDialog awaitDialog(Context context) { ProgressBar bar = new ProgressBar(context); bar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); AlertDialog dialog = new AlertDialog.Builder(context).create(); dialog.setCancelable(false); dialog.show(); Window window = dialog.getWindow(); WindowManager.LayoutParams params = window.getAttributes(); params.width = 50; params.height = 50; window.setAttributes(params); window.setContentView(bar); return dialog; } /** * * @param outFile * 写入 * @param inFile * 读取 * @throws FileNotFoundException */ private boolean fileCopy(File outFile, File inFile) throws IOException { if (outFile == null || inFile == null) { return false; } boolean isOk = true; FileChannel inChannel = new FileInputStream(inFile).getChannel();// 只读 FileChannel outChannel = new FileOutputStream(outFile).getChannel();// 只写 try { long size = inChannel.transferTo(0, inChannel.size(), outChannel); if (size <= 0) { isOk = false; } } catch (IOException e) { isOk = false; e.printStackTrace(); } finally { if (inChannel != null) { inChannel.close(); } if (outChannel != null) { outChannel.close(); } } return isOk; } private class DialogClick implements DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { if (which == -1) {// 确定 if (choicePostion < 0) { Toast.makeText(mContext, "选择数据库", Toast.LENGTH_SHORT) .show(); return; } String sp = File.separator; String folderName = fileList[choicePostion]; String backUpPath = Environment.getExternalStorageDirectory() + sp + appName + sp + BACK_FOLDER + sp + folderName; File file = new File(backUpPath); if (file.isDirectory()) { File[] files = file.listFiles(); boolean isOk = false; for (int i = 0; i < files.length; i++) { File f = files[i]; isOk = restore(f.getName(), f); if (!isOk) { String fail_msg = "恢复失败" + ":" + f.getName(); Toast.makeText(mContext, fail_msg, Toast.LENGTH_SHORT).show(); return; } } if (isOk) { // 如果有数据体现则需要刷新出新的数据 } } } else if (which == -2) {// 取消 } else if (which >= 0) { choicePostion = which; } } } /** * 执行任务 * * @author Administrator * */ private class ExecuteTask extends AsyncTask<Character, Void, Boolean> { @Override protected Boolean doInBackground(Character... params) { char c = params[0]; if (c == 'B') { backUp(); } return null; } @Override protected void onPostExecute(Boolean result) { super.onPostExecute(result); if (dialog != null) { dialog.dismiss(); } } } }
试试其它关键字
数据库备份
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
523450269
贡献的其它代码
(
1
)
.
数据库备份和恢复
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3