NSFileManager(文件管理)

来源:互联网 发布:知乎 国家开发投资集团 编辑:程序博客网 时间:2024/06/07 03:14

NSFileManager 文件管理的类继承于NSObject。连接苹果官方文档:

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/

//创建对象   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
0 0