2013-08-30 55 views
1

我想檢查文檔文件夾中的一個文件。如果此文件不存在於文檔文件夾中,請將其從主包複製到文檔文件夾。我寫這個代碼和這個文件是複製,但我的問題在這裏..... !!!!我的文件是.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]; 
    } 

} 
+1

顯示DataName'的'定義。還使用'NSLog()'記錄產生的'FileDB'字符串。也不要傳遞'error:nil';使用這種機制來獲得更好的錯誤報告...這是值得的2分鐘的努力。 – trojanfoe

+0

1.你應該用小寫字母開始方法名稱。 2.如果你傳遞一個指向非對象的指針,不要使用'nil',而是使用'NULL'。 – 2013-08-30 13:59:40

+0

它可能正在複製之前在您的軟件包中的舊文件,或者它可能在您的文檔文件夾中保留了較舊的副本,並且未替換它。在複製之前,您可能需要清除文檔文件夾。 – ApolloSoftware

回答

3

我不認爲您的文檔文件夾正在更新,因爲該數據庫的舊版本存在。您可以通過以下方法清除您的文件目錄下,並將其添加到您的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]; 
    } 
} 
+0

如果這是真的,那麼'NSLog(@「File Exist」);'火? – trojanfoe

+1

爲什麼要在每次加載此視圖時清除'Documents'文件夾中的所有文件? – rmaddy

+0

我有一個與sqlite.DB的舊副本上面的EXACT相同的問題。症狀聽起來很熟悉,因此清除是它所要求的。每次加載視圖時都不一定需要加載,而如果您執行一些簡單的邏輯(如比較兩個數據庫的時間戳或大小),則可以基於該結果複製新副本以代替每次,如需要。 Documents目錄因爲留下一堆文件而臭名昭着。 – ApolloSoftware

0

這是我的解決方案

-(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); 
      } 
    } 
    } 
相關問題