zoukankan      html  css  js  c++  java
  • oc 根据文件路径获取文件大小

    第一种封装:

    -(NSInteger)getSizeOfFilePath:(NSString *)filePath{
        /** 定义记录大小 */
        NSInteger totalSize = 0;
        /** 创建一个文件管理对象 */
        NSFileManager * manager = [NSFileManager defaultManager];
        /**获取文件下的所有路径包括子路径 */
        NSArray * subPaths = [manager subpathsAtPath:filePath];
        /** 遍历获取文件名称 */
        for (NSString * fileName in subPaths) {
            /** 拼接获取完整路径 */
            NSString * subPath = [filePath stringByAppendingPathComponent:fileName];
            /** 判断是否是隐藏文件 */
            if ([fileName hasPrefix:@".DS"]) {
                continue;
            }
            /** 判断是否是文件夹 */
            BOOL isDirectory;
            [manager fileExistsAtPath:subPath isDirectory:&isDirectory];
            if (isDirectory) {
                continue;
            }
            /** 获取文件属性 */
            NSDictionary *dict = [manager attributesOfItemAtPath:subPath error:nil];
            /** 累加 */
            totalSize += [dict fileSize];
            
        }
        /** 返回 */
        return totalSize;
    }

    第二种  block:

    /** 根据文件路径删除文件 */
    +(void)removeDirectoryPath:(NSString *)directoryPath{
    
        /** 创建文件管理者 */
        NSFileManager * manager = [NSFileManager defaultManager];
        /** 判断文件路径是否存在 */
        BOOL isDirectory;
        BOOL isExist = [manager fileExistsAtPath:directoryPath isDirectory:&isDirectory];
        if (!isDirectory||!isExist) {
            /** 提示错误信息 */
            @throw [NSException exceptionWithName:NSStringFromClass(self) reason:@"文件路径错误!" userInfo:nil];
        }
        /** 删除文件 */
        [manager removeItemAtPath:directoryPath error:nil];
        /** 创建文件 */
        [manager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    /** 根据文件路径获取文件大小 */
    +(void)getSizeOfFilePath:(NSString *)filePath completion:(void (^)(NSInteger totalSize))completion{
        
        /** 创建文件管理者 */
        NSFileManager * manager = [NSFileManager defaultManager];
        /** 判断文件路径是否存在 */
        BOOL isDirectory;
        BOOL isExist = [manager fileExistsAtPath:filePath isDirectory:&isDirectory];
        if (!isDirectory||!isExist) {
            /** 提示错误信息 */
            @throw [NSException exceptionWithName:NSStringFromClass(self) reason:@"文件路径错误!" userInfo:nil];
        }
        /** 开启子线程因以下是耗时操作 */
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            /** 定义记录文件大小 */
            NSInteger totalSize = 0;
            
            /** 获取文件 */
            NSArray * subPaths = [manager subpathsAtPath:filePath];
            /** 遍历文件 */
            for (NSString * fileName in subPaths) {
                /** 拼接完整路径 */
                NSString * subPath = [filePath stringByAppendingPathComponent:fileName];
                /** 判断是否是隐藏.DS_Store */
                if ([subPath hasPrefix:@".DS"]) {
                    continue;
                }
                /** 判断是否是文件夹 */
                BOOL isDirectory;
                [manager fileExistsAtPath:subPath isDirectory:&isDirectory];
                if (isDirectory) {
                    continue;
                }
                /** 获取文件属性 */
                NSDictionary * dic = [manager attributesOfItemAtPath:subPath error:nil];
                totalSize += [dic fileSize];
            }
             /** 回到主线程 */
            dispatch_sync(dispatch_get_main_queue(), ^{
                /** block不为空 */
                if (completion) {
                    completion(totalSize);
                }
            });
        });
        
    }
  • 相关阅读:
    Tensorflow中实现BN为什么需要加入这个额外依赖?见CS231N作业源码
    为何神经网络权重初始化要随机初始化,不能以0为初始化
    Batch Normalization&Dropout浅析
    Git版本回退和撤销修改的区别
    linux下安装git提示”无法打开锁文件 /var/lib/dpkg/lock
    数据特征选择法
    深度学习笔记整理
    全面掌握IO(输入/输出流)
    startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVI
    LitePal——Android数据库框架完整使用手册
  • 原文地址:https://www.cnblogs.com/fleas/p/5723967.html
Copyright © 2011-2022 走看看