当前位置: 移动技术网 > IT编程>移动开发>IOS > ios下移动文件方法汇总

ios下移动文件方法汇总

2019年07月24日  | 移动技术网IT编程  | 我要评论

飞向太空2002,吉他和弦大全,pixxx鹏哥的爱

这段objective c代码用于移动指定路径下的文件

复制代码 代码如下:

if ([filemanager copyitematpath:@"filepath1"
  topath:@"filepath2"  error:null]) {
     nslog(@"copied successfully");
  }

方法二

使用 nsfilemanager:
让您的文档的路径和您的缓存路径。遍历所有的文件,并将它们移动使用 nsfilemanager

复制代码 代码如下:

- (void) movealldocs {
    nsfilemanager *filemanager = [nsfilemanager defaultmanager];
    nserror *error = nil;
    nsstring *sourcedirectory = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) lastobject];
    nsstring *destinationdirectory = [nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes) lastobject];
    nsarray *contents = [filemanager contentsofdirectoryatpath:sourcedirectory error:&error];
    for(nsstring *sourcefilename in contents) {
        nsstring *sourcefile = [sourcedirectory stringbyappendingpathcomponent:sourcefilename];
        nsstring *destfile = [destinationdirectory stringbyappendingpathcomponent:sourcefilename];
        if(![filemanager moveitematpath:sourcefile topath:destfile error:&error]) {
            nslog(@"error: %@", error);
        }
    }
}

方法三

fcfilemanager 是一个构建在 nsfilemanager 之上的 ios 文件管理工具,简化了文件管理。它提供了许多静态方法,用于执行最常用的操作用几行代码。它的工作原理是默认的文件目录,允许使用相对路径,但它可以在任何其他目录中轻松工作。

move file:

复制代码 代码如下:

[fcfilemanager moveitematpath:@"test.txt" topath:@"tests/test.txt"];

remove file:

复制代码 代码如下:

//remove file at the specified path
[fcfilemanager removeitematpath:@"test.txt"];

以上所述上就是本文的全部内容了,希望大家能够喜欢。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网