这是将我的数据保存/复制到 SD 卡的代码,当我单击备份按钮时,我的数据库保存在 NOTEIT 目录中的 sdcard 中,现在我想在单击恢复按钮到我的默认目录时恢复此数据库,所以任何人都可以告诉我如何做到这一点?
public static void backupDatabase() throws IOException
{
try
{
File dbFile = new File(Environment.getDataDirectory() + "/data/com.neelrazin.noteit/databases/data");
File exportDir = new File(Environment.getExternalStorageDirectory()+"/NOTEIT");
if (!exportDir.exists())
{
exportDir.mkdirs();
}
File file = new File(exportDir, dbFile.getName());
file.createNewFile();
FileChannel inChannel = new FileInputStream(dbFile).getChannel(); //fails here
FileChannel outChannel = new FileOutputStream(file).getChannel();
try
{
inChannel.transferTo(0, inChannel.size(), outChannel);
}
finally
{
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}