Objective-C NSFileManager的使用 各种文件操作所有方法具体实现

写在前面 弄了下个人站...防止内容再次被锁定...所有东西都在这里面 welcome~ 个人博客

所有方法

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

//创建文件夹
- (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;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏学海无涯

iOS开发之城市选择器

城市选择器,借助于UIPickerView来实现,第一列为省份,第二列为第一列省份对应的城市或者区,数据放在plist中,plist结构如下图所示,第一层是一个...

38480
来自专栏陈满iOS

[iOS学习笔记]·FMDB:第三方数据库处理框架·用法示例篇(附源码Demo)

FMDatabase的实例对象_db在执行SQL语句的时候采取类似下面的代码。其中,有两种风格,一种在executeUpdate方法后面直接写上字符串的字面量语...

19420
来自专栏岑志军的专栏

iOS判断运营商类型

28770
来自专栏慎独

UIViewController生命周期分析

17940
来自专栏DannyHoo的专栏

NSOperation的简单使用代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/...

12910
来自专栏DannyHoo的专栏

xml解析代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/...

17930
来自专栏岑志军的专栏

简单的计步器实现

17830
来自专栏Code_iOS

iOS 监听设备方向旋转(iOS 9)

http://blog.csdn.net/jpcfei/article/details/8995531

21030
来自专栏Alice

从网络上下载省份城市名称并存入文件然后进行读取省份城市

//实现的功能是 从后台拿到城市的省份以及名称,然后保存在本地的沙盒中 在使用的时候再拿出来用。 步骤1 //向后台请求数据 //忽略缓存 [Requ...

21550
来自专栏Alice

iOS 获取通讯录里边的电话号码AddressBook

1  首先导入库 <AddressBook/AddressBook.h> 2 然后在导入#import <AddressBook/AddressBook.h>文...

306100

扫码关注云+社区

领取腾讯云代金券