iOS疯狂详解之NSFileManager使用

来源:互联网 发布:复杂网络点边 编辑:程序博客网 时间:2024/06/05 20:53

NSFileManager 是一个对文件进行操作的类

可以创建文件夹,移动文件夹,复制文件夹等.

//  创建文件- (void)createFile{    // 获取Documents文件路径#define kDocumentsPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]        //  拼接路径    NSString *filePath = [kDocumentsPath stringByAppendingPathComponent:@"Download"];    NSLog(@"%@",filePath);    //  获取 操作文件对象    NSFileManager *fileManger = [NSFileManager defaultManager];        //  withIntermediateDirectories    //  YES 如果不存在 创建 可以覆盖 反之 不可以覆盖(创建失败)    BOOL isCreateFile = [fileManger createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];        NSLog(@"%d",isCreateFile);}//  移动- (void)moveFile{    //  拼接老路径    NSString *old = [kDocumentsPath stringByAppendingPathComponent:@"Download"];    //  拼接新路径    NSString *new = [kCachesPath stringByAppendingPathComponent:@"Download"];    NSFileManager *fileManger = [NSFileManager defaultManager];        BOOL isMoved = [fileManger moveItemAtPath:old toPath:new error:nil];    NSLog(@"%d",isMoved);}//  复制- (void)copyFile{    //  拼接老路径    NSString *old = [kDocumentsPath stringByAppendingPathComponent:@"Download"];    //  拼接新路径    NSString *new = [kCachesPath stringByAppendingPathComponent:@"Download"];    NSFileManager *fileManger = [NSFileManager defaultManager];        BOOL isCopy = [fileManger copyItemAtPath:new toPath:old error:nil];    NSLog(@"%d",isCopy);}//  删除- (void)deleteFile{    //  拼接新路径    NSString *new = [kCachesPath stringByAppendingPathComponent:@"Download"];    NSFileManager *fileManger = [NSFileManager defaultManager];        BOOL isDelete = [fileManger removeItemAtPath:new error:nil];    NSLog(@"%d",isDelete);    }//  是否存在- (void)isExistFile{    //  拼接老路径    NSString *old = [kDocumentsPath stringByAppendingPathComponent:@"Download"];    NSFileManager *fileManger = [NSFileManager defaultManager];    BOOL isExist = [fileManger fileExistsAtPath:old];    NSLog(@"%d",isExist);}

综上:NSFileManager这个类是一个单例类 可以对文件进行操作.

0 0