美文网首页
NSFileManger 的部分方法解读

NSFileManger 的部分方法解读

作者: 海森V | 来源:发表于2018-07-02 14:08 被阅读33次

    1 - (nullable NSDirectoryEnumerator<NSURL *> *)enumeratorAtURL:(NSURL *)url includingPropertiesForKeys:(nullable NSArray<NSURLResourceKey> *)keys options:(NSDirectoryEnumerationOptions)mask errorHandler:(nullable BOOL (^)(NSURL *url, NSError *error))handler API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

    参数一: 目录地址
    参数二:从子目录的URL中预取和缓存的资源属性,如果为空则只返回子目录的地址,没有属性
    参数三:遍历时选项,如不遍历隐藏文件NSDirectoryEnumerationSkipsHiddenFiles,不遍历子文件夹NSDirectoryEnumerationSkipsSubdirectoryDescendants

    2 获取目录下子文件的地址和子文件的属性 (来自SDImageCache下 deleteOldFilesWithCompletionBlock方法)

    //获取地址
    NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
    //设置预取的属性
    NSArray<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey];
    // 获取地址和文件属性
    NSDirectoryEnumerator *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL
                                                       includingPropertiesForKeys:resourceKeys
                                                                          options:NSDirectoryEnumerationSkipsHiddenFiles
                                                                     errorHandler:NULL];
    //遍历
    for (NSURL *fileURL in fileEnumerator) {
       NSError *error;
       NSDictionary<NSString *, id> *resourceValues = [fileURL resourceValuesForKeys:resourceKeys error:&error];
       NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
    }
    

    3 判断路径下是否存在文件并且是文件夹

    • (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(nullable BOOL *)isDirectory;

    参数一:路径
    参数二:是不是文件夹

    4 创建一个文件夹的时候,判断文件夹是否存在,如果存在文件则把文件删除,如果不存在文件则创建文件夹

    + (NSString *)directoryPathWithBasePath:(NSString *)basePath relativePath:(NSString *)relativePath {
        NSString *directoryPath = nil;
        
        if (!!basePath.length) {
            directoryPath = [basePath stringByAppendingPathComponent:relativePath];
            
            BOOL needCreateDirectory = YES;
            BOOL isDirectory = NO;
            if ([[NSFileManager defaultManager] fileExistsAtPath:directoryPath isDirectory:&isDirectory]) {
                if (isDirectory) {
                    needCreateDirectory = NO;
                } else {
                    [[NSFileManager defaultManager] removeItemAtPath:directoryPath error:nil];
                }
            }
            
            if (needCreateDirectory) {
                [[NSFileManager defaultManager] createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
            }
        }
        
        return directoryPath;
    }
    

    在我写做测试的时候发现,苹果部分情况下会自动把文件的扩展名加上,如有名字为a的图片,它的地址是 **/a.png

    5 - (nullable NSArray<NSString *> *)subpathsAtPath:(NSString *)path;

    返回一个数组,数组包含 path路径下所有的子文件和递归所有字文件的路径的地址

    缺点不能过滤隐藏文件,不能配置是否遍历子文件,所以建议上面的第一种方法

    6 获取当前App下可用的硬盘大小

    + (NSNumber *)freeDiskSpace
    {
        NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
        return [fattributes objectForKey:NSFileSystemFreeSize];
    }
    

    7 获取手机的总硬盘大小

    #include <sys/mount.h>
    
    + (UInt64)totalDiskSpaceSize {
        struct statfs tStats;
        statfs("/var", &tStats);
        return (tStats.f_blocks * tStats.f_bsize);
    }
    

    相关文章

      网友评论

          本文标题:NSFileManger 的部分方法解读

          本文链接:https://www.haomeiwen.com/subject/gxlmyftx.html