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

  • 相关阅读:
    Bitcoin core核心客户端在CentOS7上的安装和配置
    Struts的标签及JSTL和EL表达式的使用总结
    Java反编译
    Tomcat 各版本 配置SSI服务实现html模块化实现#include virtual="static/_header.html"
    33
    SQL Server Management Studio无法记住密码
    SQL Server 创建角色和账号
    JS 取出DataGrid 列
    关于HTTP协议的小实验
    DNS服务操作小实验
  • 原文地址:https://www.cnblogs.com/easonoutlook/p/2642788.html
Copyright © 2011-2022 走看看