NSFileManager 文件管理的类继承于NSObject。连接苹果官方文档:
//创建对象
NSFileManager *fileManager = [NSFileManager defaultManager];
// 判断在某个路径下的文件是否存在
if (![fileManager fileExistsAtPath:[self getPath]]) {
// 如果这个目录不存在就createDirectoryAtPath
[ fileManager createDirectoryAtPath:[self getPath] withIntermediateDirectories:YES attributes:nil error:nil];
}
//获取源文件路径,假设源文件是001.rdfd
NSString *path1 = [[NSBundle mainBundle]pathForResource:@"001" ofType:@"rtfd"];
//copy
[fileManager copyItemAtPath:path1 toPath:[self getPath2] error:nil];
//move
[fileManager moveItemAtPath:[self getPath2] toPath:[self getPath3] error:nil];
NSLog(@"%@",[self getPath]);
// remove
[fileManager removeItemAtPath:[self getPath3] error:nil];
}
- (NSString *)getPath{
NSString *str = [NSString stringWithFormat:@"%@/Documents/123",NSHomeDirectory()];
return str;
}
- (NSString *)getPath2{
NSString *str = [NSString stringWithFormat:@"%@/Documents/test.rtf",NSHomeDirectory()];
return str;
}
- (NSString *)getPath3{
return [NSString stringWithFormat:@"%@/Documents/123/test.rtf",NSHomeDirectory()];
}
@end