zoukankan      html  css  js  c++  java
  • NSFileManager的基本使用

    沙盒机制

    每一个iOS应用程序都会为自己创建一个文件系统目录,这个独立、封闭、安全的空间叫做沙盒。沙盒就是一种安全体系,它规定了应用程序只能在自己的文件系统目录内访问文件,不可以访问其他应用沙盒内的内容。所有的非代码文件都保存在这个地方。

    沙盒根目录结构:Documents、Library(Caches、Perferences)、temp

    • Documents:保存应用运行时生成的需要持久化的数据,iTunes备份和恢复的时候会包括此目录,所以苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下。

    • Library

      • Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出后删除 。一般存放体积比较大,不是特别重要的资源。
      • Preferences:保存APP的所有偏好设置,iOS的Settings(设置)应用会在该目录中查找应用的设置信息,iTunes会自动备份该目录。注意:通过NSUserDefaults类来读取和设置。
    • tmp:保存应用运行时所需的临时数据,这个可以放一些当APP退出后不再需要的文件。应用没有运行时,系统也有可能会清除该目录下的文件,iTunes不会同步该目录。iPhone重启时,该目录下的文件会被删除。

    获取相关目录

    // 获取Document目录
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
     
    // 获取Library目录
    NSString *LibraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
     
    // 获取Caches目录
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
     
    // 获取Preferences目录 通常情况下,Preferences有系统维护,所以我们很少去操作它。
    NSString *preferPath = [LibraryPath stringByAppendingPathComponent:@"Preferences"];
    
    // 获取tmp目录
    NSString *tmpPath = NSTemporaryDirectory();
    

    创建文件夹

    +(BOOL)creatDir:(NSString *)path{
        if (path.length==0) {
            return NO;
        }
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL isSuccess = YES;
        BOOL isExist = [fileManager fileExistsAtPath:path];
        if (isExist==NO) {
            NSError *error;
            if (![fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) {
                isSuccess = NO;
                NSLog(@"creat Directory Failed:%@",[error localizedDescription]);
            }
        }
        return isSuccess;
    }
    

    创建文件

    +(BOOL)creatFile:(NSString*)filePath{
        if (filePath.length==0) {
            return NO;
        }
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:filePath]) {
            return YES;
        }
        NSError *error;
        NSString *dirPath = [filePath stringByDeletingLastPathComponent];
        BOOL isSuccess = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
        if (error) {
            NSLog(@"creat File Failed:%@",[error localizedDescription]);
        }
        if (!isSuccess) {
            return isSuccess;
        }
        isSuccess = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
        return isSuccess;
    }
    

    写入数据

    +(BOOL)writeToFile:(NSString*)filePath contents:(NSData *)data{
        if (filePath.length==0) {
            return NO;
        }
        BOOL result = [self creatFile:filePath];
        if (result) {
            if ([data writeToFile:filePath atomically:YES]) {
                NSLog(@"write Success");
            }else{
                NSLog(@"write Failed");
            }
        }
        else{
            NSLog(@"write Failed");
        }
        return result;
    }
    

    追加写入数据

    +(BOOL)appendData:(NSData*)data withPath:(NSString *)filePath{
        if (filePath.length==0) {
            return NO;
        }
        BOOL result = [self creatFile:filePath];
        if (result) {
            NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
            [handle seekToEndOfFile];
            [handle writeData:data];
            [handle synchronizeFile];
            [handle closeFile];
        }
        else{
            NSLog(@"appendData Failed");
        }
        return result;
    }
    

    读文件数据

    +(NSData*)readFileData:(NSString *)path{
        NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
        NSData *fileData = [handle readDataToEndOfFile];
        [handle closeFile];
        return fileData;
    }
    

    获取文件夹下的所有文件列表

    +(NSArray*)getFileList:(NSString*)path{
        if (path.length==0) {
            return nil;
        }
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *fileList = [fileManager contentsOfDirectoryAtPath:path error:&error];
        if (error) {
            NSLog(@"getFileList Failed:%@",[error localizedDescription]);
        }
        return fileList;
    }
    
    

    获取文件夹下所有文件(深度遍历)

    +(NSArray*)getAllFileList:(NSString*)path{
        if (path.length==0) {
            return nil;
        }
        NSArray *fileArray = [self getFileList:path];
        NSMutableArray *fileArrayNew = [NSMutableArray array];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        for (NSString *aPath in fileArray) {
            NSString * fullPath = [path stringByAppendingPathComponent:aPath];
            BOOL isDir = NO;
            if ([fileManager fileExistsAtPath:fullPath isDirectory:&isDir]) {
                if (isDir) {
                    [fileArrayNew addObjectsFromArray:[self getAllFileList:fullPath]];
                }else{
                    [fileArrayNew addObject:fullPath];
                }
            }
        }
        return fileArrayNew;
    }
    

    移动文件

    +(BOOL)moveFile:(NSString *)fromPath toPath:(NSString *)toPath toPathIsDir:(BOOL)dir{
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if (![fileManager fileExistsAtPath:fromPath]) {
            NSLog(@"Error: fromPath Not Exist");
            return NO;
        }
        BOOL isDir = NO;
        BOOL isExist = [fileManager fileExistsAtPath:toPath isDirectory:&isDir];
        if (isExist) {
            if (isDir) {
                if ([self creatDir:toPath]) {
                    NSString *fileName = fromPath.lastPathComponent;
                    toPath = [toPath stringByAppendingPathComponent:fileName];
                    return [self moveItemAtPath:fromPath toPath:toPath];
                }
            }else{
                [self removeFile:toPath];
                return [self moveItemAtPath:fromPath toPath:toPath];
            }
        }
        else{
            if (dir) {
                if ([self creatDir:toPath]) {
                    NSString *fileName = fromPath.lastPathComponent;
                    toPath = [toPath stringByAppendingPathComponent:fileName];
                    return [self moveItemAtPath:fromPath toPath:toPath];
                }
            }else{
                return [self moveItemAtPath:fromPath toPath:toPath];
            }
        }
        return NO;
    }
    +(BOOL)moveItemAtPath:(NSString*)fromPath toPath:(NSString*)toPath{
        BOOL result = NO;
        NSError * error = nil;
        NSFileManager *fileManager = [NSFileManager defaultManager];
            result = [fileManager moveItemAtPath:fromPath toPath:toPath error:&error];
        if (error){
            NSLog(@"moveFile Fileid:%@",[error localizedDescription]);
        }
        return result;
    }
    

    删除文件

    +(BOOL)removeFile:(NSString*)filePath{
        BOOL isSuccess = NO;
        NSError *error;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        isSuccess = [fileManager removeItemAtPath:filePath error:&error];
        if (error) {
            NSLog(@"removeFile Field:%@",[error localizedDescription]);
        }else{
            NSLog(@"removeFile Success");
        }
        return isSuccess;
    }
    

    删除某些后缀的文件

    +(void)removeFileSuffixList:(NSArray<NSString*>*)suffixList filePath:(NSString*)path deep:(BOOL)deep{
        NSArray *fileArray = nil;
        if (deep) {  // 是否深度遍历
            fileArray = [self getAllFileList:path];
        }else{
            fileArray = [self getFileList:path];
            NSMutableArray *fileArrayTmp = [NSMutableArray array];
            for (NSString *fileName in fileArray) {
                NSString* allPath = [path stringByAppendingPathComponent:fileName];
                [fileArrayTmp addObject:allPath];
            }
            fileArray = fileArrayTmp;
        }
        for (NSString *aPath in fileArray) {
            for (NSString* suffix in suffixList) {
                if ([aPath hasSuffix:suffix]) {
                    [self removeFile:aPath];
                }
            }
        }
    }
    

    获取文件大小

    +(long long)getFileSize:(NSString*)path{
        unsigned long long fileLength = 0;
        NSNumber *fileSize;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
        if ((fileSize = [fileAttributes objectForKey:NSFileSize])) {
            fileLength = [fileSize unsignedLongLongValue]; //单位是 B
        }
        return fileLength;
    }
    

    获取文件的信息

    +(NSDictionary*)getFileInfo:(NSString*)path{
        NSError *error;
        NSDictionary *reslut =  [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];
        if (error) {
            NSLog(@"getFileInfo Failed:%@",[error localizedDescription]);
        }
        return reslut;
    }
    

    参考地址:iOS 关于文件操作 NSFileManager

  • 相关阅读:
    Jenkins服务器磁盘空间爆满问题解决
    U3D GPU蒙皮
    关于STRUCT优化的一个点
    UNITY优化资料收集
    U3D的结构体堆分配栈分配
    【转】UGUI研究院之缓存策略让UI打开更快(三十)
    关于U3D场景烘焙的一个想法
    【摘】如果医生给你的孩子开这些药,请主动说不!
    【转】投影矩阵的推导
    Optimizing graphics performance
  • 原文地址:https://www.cnblogs.com/GoodmorningMr/p/11589830.html
Copyright © 2011-2022 走看看