IOS开发之文件操作(一个简单的文件操作类)
2014-06-30 15:20
495 查看
在开发应用程序中,不可避免的会使用到文件读写操作,如何才能高效省力的来处理这些操作呢!那就是把一些常用的文件操作流程写进一个工具类中,每次要用的时候
就直接导入文件,接口调用就可以啦!下面是我写的一个文件操作类。
是不是很简单粗暴啊!:)
就直接导入文件,接口调用就可以啦!下面是我写的一个文件操作类。
#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
是不是很简单粗暴啊!:)
相关文章推荐
- 今天学了一个简单的新技能Xcode6以后创建工程后没有.pch文件,所以来个技能--iOS开发
- 16—【Android游戏开发十六】Android Gesture之【触摸屏手势识别】操作!利用触摸屏手势实现一个简单切换图片的功能
- 【转载】关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 高级操作
- ios开发之IOS沙盒(sandbox)机制和文件操作
- 一个c#对操作系统文件简单操作的类
- 关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 高级操作
- 【Android游戏开发十六】Android Gesture之【触摸屏手势识别】操作!利用触摸屏手势实现一个简单切换图片的功能!
- ios 简单的plist文件读写操作(Document和NSUserDefaults)
- ios开发对配置文件plist的操作
- iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例
- 【Android游戏开发十六】Android Gesture之【触摸屏手势识别】操作!利用触摸屏手势实现一个简单切换图片的功能!
- 【Android游戏开发十六】Android Gesture之【触摸屏手势识别】操作!利用触摸屏手势实现一个简单切换图片的功能!
- IOS开发 沙盒中的文件操作
- 发布一个简单实用的xml文件操作帮助类
- 【Android游戏开发十六】Android Gesture之【触摸屏手势识别】操作!利用触摸屏手势实现一个简单切换图片的功能!
- 【iOS-Cocos2d游戏开发之二十】精灵的基础知识点总汇(位图操作/贴图更换/重排z轴等)及制作简单遮盖层!
- 【iOS-Cocos2d游戏开发之二十】精灵的基础知识点总汇(位图操作/贴图更换/重排z轴等)以及利用CCSprite与CCLayerColor制作简单遮盖层!
- 简单封装的一个文件操作的类【原创】
- iOS-Cocos2d游戏开发】使用plist文件制作简单精灵
- [ IOS-Cocos2d-x (Box2d)游戏开发之一] -简单创建一个Box2D