首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSFileManager createFile失败

NSFileManager createFile失败
EN

Stack Overflow用户
提问于 2017-07-06 07:52:18
回答 1查看 1.2K关注 0票数 0
代码语言:javascript
复制
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
}

如果我检查过的文件不存在,那么我想要创建文件为filepath。

我的设备有时成功,有时失败

Filepath如下:

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

有人能帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-06 08:33:02

我更新了你的一些代码。我为我妥善分配了NSFilemanager及其工作。您可以在文件路径或filemanager对象中出现问题。

代码语言:javascript
复制
 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];            
        }
    }

我还在模拟器和设备中检查了这段代码。

用于将字符串写入文件:

代码语言:javascript
复制
NSError *error;
NSString *stringToWrite = @"1\n2\n3\n4";
[stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44942871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档