·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> ios下移动文件方法汇总

ios下移动文件方法汇总

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

这段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"];

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