白羽
2018-06-07
来源 :网络
阅读 1923
评论 0
摘要:本文将带你了解IOS源码之下移动文件方法汇总,希望本文对大家学IOS有所帮助。
这段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"];
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号