• Objective-C 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;
    }
    
  • 相关阅读:
    MySQL函数大全
    Hibernate的理论知识点
    捕获异常
    重定向到其他的页面
    Jquery中val、text、html的区别
    条件注释判断浏览器<!--[if !IE]><!--[if IE]><!--[if lt IE 6]><!--[if gte IE 6]>
    inline-block元素的4px空白间距解决方案
    img标签中alt属性与title属性
    3像素文本偏移bug 解决方案
    google Ip
  • 原文地址:https://www.cnblogs.com/gongxiaokai/p/7123825.html
Copyright © 2020-2023  润新知