在开发应用程序中,不可避免的会使用到文件读写操作,如何才能高效省力的来处理这些操作呢!那就是把一些常用的文件操作流程写进一个工具类中,每次要用的时候
就直接导入文件,接口调用就可以啦!下面是我写的一个文件操作类。
- #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
是不是很简单粗暴啊!:)