【问题标题】:how to copy file from main bundle to Document Folder如何将文件从主包复制到文档文件夹
【发布时间】:2013-09-03 06:55:52
【问题描述】:

我想检查文档文件夹中的一个文件。如果该文件在文件夹中不存在,我希望将其从主包复制到文档文件夹。我写了这段代码和这个文件要复制,但我的问题在这里.....!!!!我的文件是 .sqlite 文件(数据库文件),当复制到文档文件夹时,自己没有数据!!!为什么???而主包中的这个文件有自己的数据。

这是我的代码,但我不知道为什么不起作用!!!

#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];
    }

}

【问题讨论】:

  • 显示DataName的定义。同时使用NSLog() 记录生成的FileDB 字符串。也不要传error:nil;使用这种机制来获得更好的错误报告……这 2 分钟的努力是值得的。
  • 1.您应该以小写字母开头的方法名称。 2.如果传递一个指向非对象的指针,不要使用nil,而是使用NULL
  • 它可能正在复制您以前在捆绑包中的旧文件,或者它可能已将旧副本保留在您的文档文件夹中并且没有替换它。您可能需要在复制之前清除文档文件夹。
  • 我好糊涂!!!!!!请帮帮我
  • [fileManager copyItemAtPath:FileDB toPath:[self DatabasePath] error:nil]; .. 使用错误来查看问题所在。

标签: ios objective-c sqlite


【解决方案1】:

我不认为您的文档文件夹正在更新,因为该数据库的旧版本存在。您可以使用以下方法清除文档目录,并将其添加到 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];
    }
}

【讨论】:

  • 如果那是真的那么NSLog(@"File Exist");不会火吗?
  • 为什么要在每次加载此视图时清除 Documents 文件夹中的所有文件?
  • 我遇到了与 sqlite.DB 的旧副本完全相同的问题。症状听起来很熟悉,因此需要进行清除。它不一定需要在每次加载视图时都加载,而是如果您执行一些简单的逻辑,例如比较两个数据库的时间戳或大小,您可以根据该结果复制新副本,而不是每次,如需要。 Documents 目录因留下一堆乱七八糟的文件而臭名昭著。
  • 另一种选择是清理您的目标,并祈祷您的数据库不会再次更改,但现在将 purge 语句保留在那里直到生产可能会有所帮助。一旦数据库停止更改,您就可以简单地删除它。
【解决方案2】:

这是我的解决方案

-(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);
            } 
    }
   }

【讨论】:

    猜你喜欢
    • 2011-03-15
    • 1970-01-01
    • 2011-03-15
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    相关资源
    最近更新 更多