代码改变世界

iphone 判断文件是否存在,如果不存在就从Bundle里面读取

2012-08-01 17:18  Mr.Xer  阅读(296)  评论(0编辑  收藏  举报

iphone 判断文件是否存在,如果不存在就从Bundle里面读取

用于把数据文件打包到工程中,然后在运行的时候判断软件的沙盒中是否有数据,如果有数据就不拷贝,如果没有数据,就要从Bundle中拷贝到软件的沙盒中。

 

 

-(BOOL) judgeFileExist:(NSString * )filePath

{

NSLog(@"%@", filePath);

 

if (![[NSFileManager defaultManagerfileExistsAtPath:filePath]) 

{

NSString *sourceFile =[[NSBundle mainBundlepathForResource:@"test.db" ofType:nil];

NSLog(@"%@, %d",sourceFile,  [sourceFile length]);

if ([sourceFile length] > 0

{

NSData *sourceData = [NSData dataWithContentsOfFile:sourceFile]; 

 

NSLog(@"%d", [sourceData length]);

 

[[NSFileManager defaultManagercreateFileAtPath:filePath contents:sourceData attributes:nil]; 

return YES;

}

}

else 

{

return YES;

}

 

return NO;

}