Objective-C的。在不存在的路径上创建文件?

时间:2013-12-06 11:49:28

标签: objective-c directory exists file-exists createfile

例如,如果我想将file.txt写入/ folder1 / folder2 / folder3 /并且没有文件夹存在。

这是手动创建它们的唯一方法吗?

3 个答案:

答案 0 :(得分:9)

尝试它应该做的工作:

    NSString * yourPath = @"/folder1/folder2/folder3";
    NSError * error = nil;
    BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath: yourPath 
                                             withIntermediateDirectories:YES 
                                                              attributes:nil 
                                                                   error:&error];
    if (!success)
      NSLog(@"Error");
    else
      NSLog(@"Success");

答案 1 :(得分:0)

以下内容对您有所帮助:

-[NSFileManager createDirectoryAtPath:withIntermediateDirectories:attributes:error:]

答案 2 :(得分:0)

您可以在中间目录参数中使用createDirectoryAtPath:withIntermediateDirectories:attributes:error:和YES来创建路径中所需的所有文件夹。

相关问题