【问题标题】:Objective-C. Create file at path which doesn't exist?目标-C。在不存在的路径上创建文件?
【发布时间】:2013-12-23 18:15:19
【问题描述】:

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

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

【问题讨论】:

  • 手动还是编程?
  • 我的意思是以编程方式但对每个文件夹的每个路径进行子串化,甚至检查每个路径是否不存在然后创建这个文件夹。或者我可以做得更简单吗?

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


【解决方案1】:

尝试它应该完成的工作:

    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");

【讨论】:

    【解决方案2】:

    以下内容将为您提供帮助:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2010-11-22
        • 2011-04-28
        • 1970-01-01
        • 2011-02-17
        • 1970-01-01
        • 2011-08-22
        • 1970-01-01
        • 1970-01-01
        • 2012-03-20
        相关资源
        最近更新 更多