iOS开发之文件操作(一个简单的文件操作类)

本文介绍了一个用于iOS开发的文件操作工具类,包括文件创建、删除、检查文件存在状态等基本功能,并提供了获取文件路径及PList文件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发应用程序中,不可避免的会使用到文件读写操作,如何才能高效省力的来处理这些操作呢!那就是把一些常用的文件操作流程写进一个工具类中,每次要用的时候

就直接导入文件,接口调用就可以啦!下面是我写的一个文件操作类。

 

#import "FileUtil.h"

@implementation FileUtil

/*文件是否存在*/
+ (BOOL)isFileExisted:(NSString *)fileName{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:[self getFilePath:fileName]]){
        return NO;
    }
    
    return YES;
}

/*创建指定名字的文件*/
+ (BOOL)createFileAtPath:(NSString *)fileName{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[array objectAtIndex:0] stringByAppendingPathComponent:fileName];
    NSLog(@"-----%@:", path);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:path]){
        [fileManager createFileAtPath:path contents:nil attributes:nil];
        return YES;
    }
    
    return NO;
}

/*创建指定名字的文件夹*/
+ (BOOL)createDirectoryAtPath:(NSString *)fileName{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[array objectAtIndex:0] stringByAppendingPathComponent:fileName];
    NSLog(@"-----%@:", path);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:path]){
        NSError *error = nil;
        [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
        return YES;
    }
    
    return NO;
}

/*得到文件路径*/
+ (NSString *)getFilePath:(NSString *)fileName{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[array objectAtIndex:0] stringByAppendingPathComponent:fileName];
    
    return path;
}

/*删除文件*/
+ (BOOL)deleteFileAtPath:(NSString *)fileName{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[array objectAtIndex:0] stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if(![fileManager fileExistsAtPath:path]){
        return NO;
    }
    
    [fileManager removeItemAtPath:path error:nil];
    return YES;
}

/*得到PList文件*/
+ (NSMutableDictionary *)getPlistFile:(NSString *)fileName{

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle pathForResource:fileName ofType:@"plist"];
    
    return [[NSMutableDictionary alloc] initWithContentsOfFile:path];
}

/*获取plist文件目录*/
+ (NSString *)getPListFilePath:(NSString *)fileName{
    NSBundle *bundle = [NSBundle mainBundle];
    return [bundle pathForResource:fileName ofType:@"plist"];
}

@end


是不是很简单粗暴啊!:)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HelloWord杰少

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值