I want to check one file in Document folder.I want if this file not exist in document folder copy it from main bundle to document folder. I write this code and this file to be copy but my problem here.....!!!! my file is .sqlite file (Database file) and when copy to document folder hasn't data in self!!!! why??? while the this file when is in main bundle has data in self.
我想检查Document文件夹中的一个文件。我想如果文件夹中不存在此文件,请将其从主包复制到文档文件夹。我写这个代码和这个文件是要复制但我的问题在这里..... !!!!我的文件是.sqlite文件(数据库文件),当复制到文件夹时自己没有数据!!!!为什么???而在main bundle中的这个文件有自己的数据。
this is my code but I don't know why not work!!!!
这是我的代码,但我不知道为什么不工作!!!!
#define DataName @"mydatabase.sqlite"
- (void)viewDidLoad
{
[self CopyDatabase];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (NSString*)DatabasePath
{
NSArray *Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DocumentDir = [Paths objectAtIndex:0];
//NSLog(@"%@",DocumentDir);
return [DocumentDir stringByAppendingPathComponent:DataName];
}
- (void)CopyDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:[self DatabasePath]];
NSString *FileDB = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DataName];
if (success)
{
NSLog(@"File Exist");
return;
}
else
{
[fileManager copyItemAtPath:FileDB toPath:[self DatabasePath] error:nil];
}
}
2 个解决方案
#1
5
I don't think your documents folder is updating because an older version of that db exists. You can purge your documents directory with the following method, and add it to the top of your viewDidLoad:
我不认为您的文档文件夹正在更新,因为该数据库的旧版本存在。您可以使用以下方法清除文档目录,并将其添加到viewDidLoad的顶部:
- (void)viewDidLoad
{
[self purgeDocumentsDirectory];
[self CopyDatabase];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)purgeDocumentsDirectory
{
NSLog(@"Purging Documents Directory...");
NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSError *error = nil;
for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error]) {
[[NSFileManager defaultManager] removeItemAtPath:[folderPath stringByAppendingPathComponent:file] error:&error];
}
}
#2
1
This is my solution
这是我的解决方案
-(void) copytoDocument {
NSString *openFile ;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pgnPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.text", @"100_Greatest_games"]];
if ([fileManager fileExistsAtPath:pgnPath] == NO)
{
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"100_Greatest_games" ofType:@"text"];
[fileManager copyItemAtPath:resourcePath toPath:pgnPath error:&error];
if (error) {
NSLog(@"Error on copying file: %@\nfrom path: %@\ntoPath: %@", error, resourcePath, pgnPath);
}
}
}
#1
5
I don't think your documents folder is updating because an older version of that db exists. You can purge your documents directory with the following method, and add it to the top of your viewDidLoad:
我不认为您的文档文件夹正在更新,因为该数据库的旧版本存在。您可以使用以下方法清除文档目录,并将其添加到viewDidLoad的顶部:
- (void)viewDidLoad
{
[self purgeDocumentsDirectory];
[self CopyDatabase];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)purgeDocumentsDirectory
{
NSLog(@"Purging Documents Directory...");
NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSError *error = nil;
for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error]) {
[[NSFileManager defaultManager] removeItemAtPath:[folderPath stringByAppendingPathComponent:file] error:&error];
}
}
#2
1
This is my solution
这是我的解决方案
-(void) copytoDocument {
NSString *openFile ;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pgnPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.text", @"100_Greatest_games"]];
if ([fileManager fileExistsAtPath:pgnPath] == NO)
{
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"100_Greatest_games" ofType:@"text"];
[fileManager copyItemAtPath:resourcePath toPath:pgnPath error:&error];
if (error) {
NSLog(@"Error on copying file: %@\nfrom path: %@\ntoPath: %@", error, resourcePath, pgnPath);
}
}
}