2017-07-06 9 views
0
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *filePath; 

if (str_FolderName!=nil){ 
    filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/%@",str_FolderName,str_FileName]]; 
} else { 
    filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",str_FileName]]; 
} 

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSMutableDictionary *data; 

if ([fileManager fileExistsAtPath: filePath]) 
{ 
    //file is exist 
    data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 

} else { 
    BOOL Success = [fileManager createFileAtPath:filePath contents:nil attributes:nil]; 
    // sometime return YES, sometime return NO <<< this is my question 
    if(Success == YES){ 
     data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 
    }else{ 
     data = [[NSMutableDictionary alloc] init];    
    } 
} 

for (uint32_t u32_Cnt=0;u32_Cnt<arr_Keys.count;u32_Cnt++) { 
    [data setValue:[NSString stringWithFormat:@"%@",[marr_Data objectAtIndex:u32_Cnt]] forKey:[arr_Keys objectAtIndex:u32_Cnt]]; 
} 

if ([data writeToFile:filePath atomically: YES]) 
{ 
    NSLog(@"[Save OK]"); 

    return DEF_PASS; 
} else { 
    NSLog(@"[Save Fail]"); <<<< now happened 
} 

失敗 NSFileManager CREATEFILEは、私は、ファイルが存在していないがチェックしている場合は、いつか、私はファイルパスとしていつか成功を</p> <p>マイデバイスをファイルを作成したい、その後、

ファイルパスは以下の通りです

を失敗しました:

@"/var/mobile/Containers/Data/Application/xxxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx/Documents/mfolder/myfile"

誰かが私を助けることができますか?

答えて

0

コードの一部を更新します。私はNSFilemanagerとその仕事の適切な割り当てをしてくれます。ファイルパスまたはファイルマネージャオブジェクトで問題を発生させることができます。

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *filePath; 

    if (str_FolderName!=nil){ 
     filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/%@",str_FolderName,str_FileName]]; 
    }else{ 
     filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",str_FileName]]; 
    } 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSMutableDictionary *data; 
    if ([fileManager fileExistsAtPath: filePath]) 
    { 
     //file is exist 
     data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 

    } 
    else 
    { 
     if (str_FolderName!=nil){ 
      NSString *strDicPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",str_FolderName]]; 
      // You need to create director before creating file. 
      if (![[NSFileManager defaultManager] fileExistsAtPath:strDicPath]) 
      { 
       NSError *error; 
       [[NSFileManager defaultManager] createDirectoryAtPath:strDicPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder 
      } 

     } 
     BOOL Success = [fileManager createFileAtPath:filePath contents:nil attributes:nil]; 
     // sometime return YES, sometime return NO <<< this is my question 
     if(Success == YES){ 
      data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 
     }else{ 
      data = [[NSMutableDictionary alloc] init];    
     } 
    } 

このコードもシミュレータとデバイスの両方でチェックします。ファイルに文字列を書き込むため

は:

NSError *error; 
NSString *stringToWrite = @"1\n2\n3\n4"; 
[stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]; 
+0

私のパスは、フォルダ名が含まれている、私はそれをテストされて別のフォルダなしで成功creatのファイルですが、... – HunterChen

+0

フォルダとファイルを作成して失敗しましたあなたはそれを与えることができますか? – Nirmalsinh

+0

filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@ "/%@ /%@"、str_FolderName、str_FileName]]; – HunterChen

関連する問題

 関連する問題