AIR项目,我们使用的是sqlite数据库,目前需要一个备份策略。想法是当窗口关闭的时候,备份数据库,由于sqlite的特性,直接复制文件即可,可在测试复制的时候遇到一系列错误和意外。
SecurityError: fileWriteResource 错误
以及注册的事件都没有反应
以下为测试成功的代码:
protected function bak_clickHandler(event:MouseEvent):void
{
//如果不使用nativePath来重新构建全路径名的新文件的话,会报SecurityError: fileWriteResource 错误
var src_file:File=new File(
File.applicationDirectory.resolvePath("bak.db").nativePath);
var deFile:File = new File(
File.applicationDirectory.resolvePath("bak/bak20121209.db")
.nativePath);
src_file.copyToAsync(deFile, true);
//复制文件 完成事件,io错误事件要使用复制的源文件注册事件
src_file.addEventListener(Event.COMPLETE, copyComplete);
src_file.addEventListener(IOErrorEvent.IO_ERROR, copyComplete);
}
//因为IOErrorEvent是Event的重孙类(-_-,就是子类的子类的子类), 所以类型就用Event
protected function copyComplete(event:Event):void
{
if(event.type==Event.COMPLETE){
Alert.show("复制完成", "");
}else{
Alert.show(event.toString(), "备份发生错误");
}
//删除注册的侦听事件
event.target.removeEventListener(event.type, copyComplete)
}