登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

Objective-C NSFileManager的使用 各种文件操作 [复制链接]

2016-12-21 22:16
攻小楷o 阅读:359 评论:1 赞:1
Tag:  NSFileManager

所有方法

都很简单,大概记录一下,写文件并没有是追加的方式而是简单的覆盖

//创建文件夹
- (BOOL)creatDir:(NSString*)newDirName at:(NSString*)dirName ;
//创建文件
- (BOOL)creatFile:(NSString*)fileName at:(NSString*)dirName ;
//写文件-字符串
- (BOOL)writeString:(NSString*)string inFilePath:(NSString*)filePath;
//写文件-字典
- (BOOL)writeDict:(NSDictionary*)dict inFilePath:(NSString*)filePath ;
//写文件-数组
- (BOOL)writeArray:(NSArray*)array inFilePath:(NSString*)filePath ;
//读文件 
- (NSString*)readFileAt:(NSString*)filePath;
//获取某个目录下所有文件
- (NSArray*)getAllFileAtDir:(NSString*)dirPath ;
//判断文件是否存在
- (BOOL)fileIsExistAtPath:(NSString*)filePath ;
//计算某个文件大小
- (unsigned long long)computerFileSizeAtPath:(NSString*)filePath;
//计算整个文件夹中所有文件大小
- (unsigned long long)computerDirSizeAtPath:(NSString*)dirPath;
//删除文件
- (BOOL)removeFileAtPath:(NSString*)filePath;
//移动文件
- (BOOL)moveFile:(NSString*)fileName fromPath:(NSString*)fromPath toPath:(NSString*)toPath;

具体实现

初始化一下NSFileManager 并获取根路径
主要是加了一些判断,代码不是很精简 不要笑我。

- (instancetype)init {
    self = [super init];
    if (self) {
        self.homePath = NSHomeDirectory();
        self.manager = [NSFileManager defaultManager];
        NSLog(@"根路径地址:%@",self.homePath);
    }
    return self;
}
//创建文件夹
- (BOOL)creatDir:(NSString*)newDirName at:(NSString*)dirName {
    NSString *path = [self.homePath stringByAppendingString:dirName];
    NSString *filePath = [path stringByAppendingPathComponent:newDirName];
    if ([self.manager fileExistsAtPath:filePath]) {
        NSLog( @"创建失败,目录已存在");
    }else {
        BOOL isCreate = [self.manager createDirectoryAtPath:filePath withIntermediateDirectories:NO attributes:nil error:nil];
        if (isCreate) {
            NSLog( @"创建成功");
            return YES;
        } else {
            NSLog( @"创建失败,请检查路径");
            return NO;
        }
    }
    return NO;
}
//创建文件
- (BOOL)creatFile:(NSString*)fileName at:(NSString*)dirName {
    //目录位置
    NSString *dirPath = [self.homePath stringByAppendingString:dirName];
    //文件位置
    NSString *filePath = [dirPath stringByAppendingPathComponent:fileName];
    BOOL isDir = NO;
    BOOL isFileExist = [self.manager fileExistsAtPath:dirPath isDirectory:&isDir];
    //目录是否存在
    if (!(isFileExist && isDir)) {
        NSLog(@"目录不存在,请先创建目录");
        return NO;
    }
    //文件是否存在
    if (![self.manager fileExistsAtPath:filePath]) {
        BOOL isCreate = [self.manager createFileAtPath:filePath contents:nil attributes:nil];
        if (isCreate) {
            NSLog(@"创建成功");
            return YES;
        } else {
            NSLog(@"创建失败");
            return NO;
        }
    }
    else {
        NSLog(@"创建失败,文件已存在");
        return NO;
    }
    return NO;
}
//写文件-字符串
- (BOOL)writeString:(NSString*)string inFilePath:(NSString*)filePath{
    NSString *path = [self.homePath stringByAppendingString:filePath];
    BOOL isFileExist = [self.manager fileExistsAtPath:path];
    if (isFileExist) {
        BOOL isWrite = [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
        if (isWrite) {
            NSLog(@"写入成功");
            return YES;
        } else {
            NSLog( @"写入失败");
            return NO;
        }
    }else{
        NSLog(@"文件不存在,请先创建文件");
        return NO;
    }
    return NO;
}
//写文件-字典
- (BOOL)writeDict:(NSDictionary*)dict inFilePath:(NSString*)filePath {
    NSString *path = [self.homePath stringByAppendingString:filePath];
    BOOL isFileExist = [self.manager fileExistsAtPath:path];
    if (isFileExist) {
        BOOL isWrite = [dict writeToFile:path atomically:YES];
        if (isWrite) {
            NSLog(@"写入成功");
            return YES;

        } else {
            NSLog(@"写入失败");
            return NO;

        }
    }else{
        NSLog(@"文件不存在,请先创建文件");
        return NO;

    }
    return NO;

}
//写文件-数组
- (BOOL)writeArray:(NSArray*)array inFilePath:(NSString*)filePath {
    NSString *path = [self.homePath stringByAppendingString:filePath];
    BOOL isFileExist = [self.manager fileExistsAtPath:path];
    if (isFileExist) {
        BOOL isWrite = [array writeToFile:path atomically:YES];
        if (isWrite) {
            NSLog(@"写入成功");
            return YES;

        } else {
            NSLog(@"写入失败");
            return NO;
        }
    }else{
        NSLog(@"文件不存在,请先创建文件");
        return NO;
    }
    return NO;
}
//读文件
- (NSString*)readFileAt:(NSString*)filePath{
    NSString *path = [self.homePath stringByAppendingString:filePath];
    NSData *data = [self.manager contentsAtPath:path];
    NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return result;
}
//获取某个目录下所有文件
- (NSArray*)getAllFileAtDir:(NSString*)dirPath {
    NSString *path = [self.homePath stringByAppendingString:dirPath];
    NSArray *dirArray = [self.manager contentsOfDirectoryAtPath:path error:nil];
    return dirArray;
}
//判断文件是否存在
- (BOOL)fileIsExistAtPath:(NSString*)filePath {
    NSString *path = [self.homePath stringByAppendingString:filePath];
    BOOL isFileExist = [self.manager fileExistsAtPath:path];
    if (isFileExist) {
        NSLog(@"文件存在");
        return YES;

    }  else {
        NSLog(@"文件不存在");
        return NO;

    }
    return NO;
}
//计算某个文件大小 单位是字节
- (unsigned long long)computerFileSizeAtPath:(NSString*)filePath {
    NSString *path = [self.homePath stringByAppendingString:filePath];
    BOOL isDir;
    BOOL isFile = [self.manager fileExistsAtPath:path isDirectory:&isDir];
    if (!isDir) {
        if (!isFile) {
            NSLog(@"文件不存在");
            return 0;

        }else {
            unsigned long long fileSize = [self.manager attributesOfItemAtPath:path error:nil].fileSize;
            return fileSize;
        }
    }  else {
        NSLog( @"该文件是一个目录");
        return 0;
    }
    return 0;
}
//计算整个文件夹中所有文件大小
- (unsigned long long)computerDirSizeAtPath:(NSString*)dirPath {
    NSString *path = [self.homePath stringByAppendingString:dirPath];
    BOOL isExist = [self.manager fileExistsAtPath:path];
    if (isExist) {
        
        NSEnumerator *childFilesEnumerator = [[self.manager subpathsAtPath:path] objectEnumerator];
        NSString* fileName;
        long long folderSize = 0;
        while ((fileName = [childFilesEnumerator nextObject]) != nil){
            NSString* fileAbsolutePath = [path stringByAppendingPathComponent:fileName];
            folderSize += [self.manager attributesOfItemAtPath:fileAbsolutePath error:nil].fileSize;
        }
        return folderSize;
        
    } else {
        NSLog(@"目录不存在");
        return 0;
    }
    return 0;
}
//删除文件
- (BOOL)removeFileAtPath:(NSString*)filePath {
    NSString *path = [self.homePath stringByAppendingString:filePath];
    if ([self.manager fileExistsAtPath:path]) {
        BOOL isRemove = [self.manager removeItemAtPath:path error:nil];
        if (!isRemove) {
            NSLog(@"移除失败");
            return NO;
        } else {
            NSLog(@"移除成功");
            return YES;
        }
    } else {
        NSLog(@"文件不存在");
        return NO;
    }  
    return NO;
}
//移动文件
- (BOOL)moveFile:(NSString*)fileName fromPath:(NSString*)fromPath toPath:(NSString*)toPath {
    NSString *fPath = [self.homePath stringByAppendingString:fromPath];
    NSString *src = [fPath stringByAppendingPathComponent:fileName];
    NSString *tPath = [self.homePath stringByAppendingString:toPath];
    NSString *des = [tPath stringByAppendingPathComponent:fileName];
    
    if (![self.manager fileExistsAtPath:src]) {
        NSLog(@"文件不存在");
        return NO;
    }else {
        if (![self.manager fileExistsAtPath:tPath]) {
            NSLog(@"目标路径不存在");
            return NO;
        } else {
            BOOL move = [self.manager moveItemAtPath:src toPath:des error:nil];
            if (move) {
                NSLog( @"移动成功");
                return YES;
            } else {
                NSLog(@"移动失败");
                return NO;
            }
        }
    }
    return NO;
}

简书看代码更舒服一点
http://www.jianshu.com/users/7897b0bd4a55/latest_articles

分享到:
我来说两句
facelist
您需要登录后才可以评论 登录 | 立即注册
所有评论(1)
Torr 2016-12-21 22:44
感谢分享
回复
关闭

每日头条

通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

返回顶部