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

  • 相关阅读:
    Web前端开发
    用javascript向一个网页连接接口发送请求,并接收该接口返回的json串
    如何在tomcat启动的时候运行一个Java类
    Linux永久挂载远程网络目录
    C/C++跨平台的的预编译宏
    利用http实现文件的上传和下载
    基于qml创建最简单的图像处理程序(1)-基于qml创建界面
    基于qml创建最简单的android机图像采集程序
    OpenCV相关网站推荐(Informative websites related to OpenCV)
    (GO_GTD_3)基于OpenCV和QT,建立Android图像处理程序
  • 原文地址:https://www.cnblogs.com/easonoutlook/p/2642788.html
Copyright © 2011-2022 走看看