OC学习:OC中对文件的操作

下面是一些简单的关于OC中对文件的基本操作的案例:

//应用程序文件的移动
    [[NSFileManager new] moveItemAtPath:(NSString *)/*路径格式(/user/……)-NSString类型*/ toPath:(NSString *) error:(NSError *__autoreleasing *)];
    //应用程序文件的复制
    [[NSFileManager new] copyItemAtPath:(NSString *) toPath:(NSString *) error:(NSError *__autoreleasing *)];
    //应用程序文件的删除
    [[NSFileManager new] removeItemAtPath:(NSString *) error:(NSError *__autoreleasing *)];
    //应用程序创建文件夹
    [[NSFileManager new] createDirectoryAtPath:(NSString *) withIntermediateDirectories:(BOOL) attributes:(NSDictionary *) error:(NSError *__autoreleasing *)];
     //应用程序创建文件
    [[NSFileManager new] createFileAtPath:(NSString *) contents:(NSData *) attributes:(NSDictionary *)];

一些必要的说明

#define fm [NSFileManager new]
#define DocumentsPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
//宏定义减少代码书写量
typedef enum {
    SuccessWriter,
    failWriter,
    failDownload,
    AlreadyDownload,
    DownloadAddressError
} DownloadType;
//枚举能够返回各种情况,比BOOL强大。
typedef enum {
    Success,
    Fail,
    NotFind
} removeType;

应用程序文件的写入

(NSString *)urlString CallFileName:(NSString *)fileName {
    DownloadType type = failDownload;
    if (urlString ==nil) {
        return DownloadAddressError;
    }
    //为了保证健壮性,进行转码,防止中文和特殊符号
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:4];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
    if (data == nil) {
        return failDownload;
    }
    if (fileName ==nil) {
        fileName = [urlString lastPathComponent];
    }
    //路径
    NSString *filePath =[DocumentsPath stringByAppendingPathComponent:fileName];
    //开始写入
    if (![fm fileExistsAtPath:filePath]) {
        [fm createFileAtPath:filePath contents:data attributes:nil];      
               type = SuccessWriter;

    }else {
        type = AlreadyDownload;
    }
    //判断写入是否成功
    if (![fm fileExistsAtPath:filePath]) {
        type = failWriter;
    }
   return type;
}

应用程序文件夹内容的删除

+(removeType) removeAllImageInDirectoryName:(NSString *)directoryName {
    //文件夹路径
    directoryName = [DocumentsPath stringByAppendingPathComponent:directoryName];
    //判断要删除的对象是否存在
    if (![fm fileExistsAtPath:directoryName]) {
        return NotFind;
    }
    //找到文件夹中的文件,返回数组
    NSArray *array = [fm subpathsAtPath:directoryName];
    //遍历数组时删除文件夹中文件
    for (NSString *obj in array) {
        [fm removeItemAtPath:[directoryName stringByAppendingPathComponent:obj] error:nil];
    }
    //判断是否删除成功
    if ([fm subpathsAtPath:directoryName].count!=0) {
        return Fail;
    }
    return Success;
}

删除指定文件

+(removeType) removeFileName:(NSString *)fileName inDirectoryName:(NSString *)directoryName {
    directoryName = [DocumentsPath stringByAppendingPathComponent:directoryName];
    fileName = [directoryName stringByAppendingPathComponent:fileName];
    if ([fm fileExistsAtPath:fileName]) {
        [fm removeItemAtPath:fileName error:nil];
    }else {
        return NotFind;
    }
    if ([fm fileExistsAtPath:fileName]) {
        return Fail;
    }
    return Success;
}

求应用文件的大小

+(NSString *) caculateSizeWithDirectoryName:(NSString *)directoryName {
    NSString *size = [NSString stringWithFormat:@"error!%@文件不存在!",directoryName];
    directoryName = [DocumentsPath stringByAppendingPathComponent:directoryName];
    double count = 0.0;
    NSArray *array = [fm subpathsAtPath:directoryName];
    for (NSString *obj in array) {
        NSString *filePath = [directoryName stringByAppendingPathComponent:obj];
        NSData *data = [NSData dataWithContentsOfFile:filePath];
        count = count + data.length;
    }
    size = [NSString stringWithFormat:@"%.4lfM",count/1024/1024];
    return size;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值