zoukankan      html  css  js  c++  java
  • NSFileManager 文件拷贝函数copyItemAtPath:toPath:error:

    1、拷贝到目标目录的时候,如果文件已经存在则会直接失败

    2、目标目录必须是文件 

    贴一段别人写 文章

    今天我用NSFileManager 文件拷贝函数copyItemAtPath:toPath:error:拷贝文件的时候,一直出现一下错误:
    Error Domain=NSPOSIXErrorDomain Code=17 “The operation couldn’t be completed. File exists” UserInfo=0x9d37050 {NSUserStringVariant=Copy, NSFilePath=/Users/kimziv/Library/Application Support/iPhone Simulator/4.2/Applications/0110275A-4939-452E-BA34-CADBB63F2042/Documents/resources/ppt/media/image1.jpeg, NSDestinationFilePath=/Users/kimziv/Library/Application Support/iPhone Simulator/4.2/Applications/0110275A-4939-452E-BA34-CADBB63F2042/Documents}
    后来仔细看官方文档发现,我是该函数的第二个参数dstPath我用错了,要以文件名结尾。

    Discussion

    If srcPath is a file, the method creates a file at dstPath that holds the exact contents of the original file (this includes BSD special files). If srcPath is a directory, the method creates a new directory at dstPath and recursively populates it with duplicates of the files and directories contained in srcPath, preserving all links. The file specified in srcPath must exist, while dstPath must not exist prior to the operation. When a file is being copied, the destination path must end in a filename—there is no implicit adoption of the source filename. Symbolic links are not traversed but are themselves copied. File or directory attributes—that is, metadata such as owner and group numbers, file permissions, and modification date—are also copied.

    NSString* fileName=@"test.jpg";
    NSString* resourceDirectory = [[NSBundle mainBundle] resourcePath];
    NSString* srcPath = [path stringByAppendingPathComponent:fileName];
    NSArray* paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory= [paths objectAtIndex:0];
    NSString* dstPath=[documentsDirectory stringByAppendingPathComponent:fileName];//这里要特别主意,目标文件路径一定要以文件名结尾,而不要以文件夹结尾
    NSError* error=nil;
    [[NSFileManager defaultManager]copyItemAtPath:srcPath toPath:dstPath error:&error ];
    if (error!=nil) {
    NSLog(@"%@", error);
    NSLog(@"%@", [error userInfo]);
    }

  • 相关阅读:
    Consul负载均衡策略记录
    ASP NET CORE开发优化相关专用随笔
    .NET CORE 3.1配置文件读取方式
    CentOS 8 安装.NET CORE 3.1 发布以及运行
    CORE EF生成ORACLE数据库模型报错问题记录
    【转载】一名程序员十年技术之路的思考与感悟
    iview-admin部署linux nginx报500错误的问题记录
    [转]浅谈账号系统设计
    C#使用phantomjs,爬取AJAX加载完成之后的页面
    nginx触屏版跟PC的代理设置
  • 原文地址:https://www.cnblogs.com/easonoutlook/p/2642788.html
Copyright © 2011-2022 走看看