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]);
    }

  • 相关阅读:
    用户管理
    grep
    Java中的Map
    WinForm------Reflector反编译工具下载
    SQLServer------解决IP地址登录不了数据库问题
    WinForm------弹出MessageBox窗口的同时隐藏当前窗口
    WinForm------DockManager控件的使用方法(里面包含DockPanel控件)
    IIS------Http错误:50019,由于权限不足无法读取配置文件
    IIS------配置错误:不能在此路径中使用此配置节
    IIS------配置.Net 4.0
  • 原文地址:https://www.cnblogs.com/easonoutlook/p/2642788.html
Copyright © 2011-2022 走看看