iOS文件处理类

这是一个用来简化iOS中关于文件操作的一个类,所有方法都为类方法.

Source

File.h

//
// File.h
// FileManager
//
// http://home.cnblogs.com/u/YouXianMing/
//
// Copyright (c) 2014年 YouXianMing All rights reserved.
// #import <Foundation/Foundation.h> #define FILE_PATH(filePath) [File path:(filePath)]
#define ROOT_PATH [File rootPath]
#define BUNDLE_PATH(fileName) [File bundleSource:(fileName)]
#define CREATE_FOLDER(folderPath) [File createFolder:(folderPath)]
#define FILE_EXIST(filePath) [File exist:(filePath)]
#define IS_DIRECTORY(filePath) [File isDirectory:(filePath)]
#define IS_FILE(filePath) ![File isDirectory:(filePath)]
#define FILE_INFO(filePath) [File fileInfo:(filePath)]
#define FILE_SIZE(filePath) [File fileSize:(filePath)]
#define FILE_DICTIONARY(filePath) [File dictionaryFrom:(filePath)]
#define FILE_ARRAY(filePath) [File arrayFrom:(filePath)] @interface File : NSObject /*
注意:凡是参数为 filePath folderPath 的全部为相对路径,用以下字符串拼接使用 /Documents
/Library/Caches
/Library/Preferences
/tmp 其他都为绝对路径
*/ + (NSString *)rootPath;
+ (NSString *)bundleSource:(NSString *)fileName;
+ (NSString *)path:(NSString *)filePath;
+ (BOOL)createFolder:(NSString *)filePath;
+ (BOOL)exist:(NSString *)filePath;
+ (BOOL)isDirectory:(NSString *)filePath;
+ (NSDictionary *)fileInfo:(NSString *)filePath;
+ (int)fileSize:(NSString *)filePath;
+ (NSArray *)enumeratorFolder:(NSString *)folderPath;
+ (void)enumeratorFolder:(NSString *)folderPath each:(void (^)(NSString *path))each; + (BOOL)copyFrom:(NSString *)sourcePath to:(NSString *)targetPath;
+ (BOOL)moveFrom:(NSString *)sourcePath to:(NSString *)targetPath;
+ (BOOL)remove:(NSString *)targetPath; + (BOOL)writePlist:(id)plist to:(NSString *)filePath;
+ (NSMutableDictionary *)dictionaryFrom:(NSString *)filePath;
+ (NSMutableArray *)arrayFrom:(NSString *)filePath;
+ (void)path:(NSString *)filePath dictionary:(void (^)(NSMutableDictionary *dictionary))dictionary;
+ (void)path:(NSString *)filePath array:(void (^)(NSMutableArray *array))array; @end

File.m

//
// File.m
// FileManager
//
// http://home.cnblogs.com/u/YouXianMing/
//
// Copyright (c) 2014年 YouXianMing All rights reserved.
// #import "File.h" static NSString *_sandBoxPath = nil; @implementation File + (void)initialize
{
if (self == [File class])
{
_sandBoxPath = NSHomeDirectory();
}
} + (NSString *)rootPath
{
return _sandBoxPath;
} + (NSString *)path:(NSString *)filePath
{
return [_sandBoxPath stringByAppendingPathComponent:filePath];
} + (BOOL)createFolder:(NSString *)filePath
{
return [[NSFileManager defaultManager] createDirectoryAtPath:[self path:filePath]
withIntermediateDirectories:YES
attributes:nil
error:nil];
} + (NSString *)bundleSource:(NSString *)fileName
{
return [[NSBundle mainBundle] pathForResource:fileName
ofType:nil];
} + (BOOL)exist:(NSString *)filePath
{
return [[NSFileManager defaultManager] fileExistsAtPath:[self path:filePath]
isDirectory:NO];
} + (BOOL)isDirectory:(NSString *)filePath
{
BOOL isDirectory = NO; [[NSFileManager defaultManager] fileExistsAtPath:[self path:filePath]
isDirectory:&isDirectory]; return isDirectory;
} + (BOOL)copyFrom:(NSString *)sourcePath to:(NSString *)targetPath
{
return [[NSFileManager defaultManager] copyItemAtPath:sourcePath
toPath:targetPath
error:nil];
} + (BOOL)moveFrom:(NSString *)sourcePath to:(NSString *)targetPath
{
return [[NSFileManager defaultManager] moveItemAtPath:sourcePath
toPath:targetPath
error:nil];
} + (BOOL)remove:(NSString *)targetPath
{
return [[NSFileManager defaultManager] removeItemAtPath:targetPath
error:nil];
} + (NSArray *)enumeratorFolder:(NSString *)folderPath
{
if ([self isDirectory:folderPath])
{
NSMutableArray *storeArray = [NSMutableArray array]; NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:folderPath];
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:docsDir]; NSString *file;
while ((file = [dirEnum nextObject]))
{
[storeArray addObject:[folderPath stringByAppendingPathComponent:file]];
} return storeArray;
}
else
{
return nil;
}
} + (void)enumeratorFolder:(NSString *)folderPath each:(void (^)(NSString *path))each
{
if ([self isDirectory:folderPath])
{
NSMutableArray *storeArray = [NSMutableArray array]; NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:folderPath];
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:docsDir]; NSString *file;
while ((file = [dirEnum nextObject]))
{
[storeArray addObject:[folderPath stringByAppendingPathComponent:file]];
} [storeArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
each(obj);
}];
}
} + (NSDictionary *)fileInfo:(NSString *)filePath
{
return [[NSFileManager defaultManager] attributesOfItemAtPath:[self path:filePath]
error:nil];
} + (int)fileSize:(NSString *)filePath
{
return [[[[NSFileManager defaultManager] attributesOfItemAtPath:[self path:filePath]
error:nil] \
objectForKey:@"NSFileSize"] intValue];
} + (BOOL)writePlist:(id)plist to:(NSString *)filePath
{
if ([plist isKindOfClass:[NSDictionary class]])
{
NSDictionary *point = plist; return [point writeToFile:[self path:filePath]
atomically:YES];
}
else if ([plist isKindOfClass:[NSArray class]])
{
NSArray *point = plist; return [point writeToFile:[self path:filePath]
atomically:YES];
}
else
{
return NO;
}
} + (NSMutableDictionary *)dictionaryFrom:(NSString *)filePath
{
NSMutableDictionary *dictionary = \
[[NSMutableDictionary alloc] initWithContentsOfFile:[self path:filePath]]; return dictionary;
} + (NSMutableArray *)arrayFrom:(NSString *)filePath
{
NSMutableArray *array = \
[[NSMutableArray alloc] initWithContentsOfFile:[self path:filePath]]; return array;
} + (void)path:(NSString *)filePath dictionary:(void (^)(NSMutableDictionary *dictionary))dictionary
{
NSMutableDictionary *sourceDictionary = \
[[NSMutableDictionary alloc] initWithContentsOfFile:[self path:filePath]]; dictionary(sourceDictionary); [sourceDictionary writeToFile:[self path:filePath]
atomically:YES];
} + (void)path:(NSString *)filePath array:(void (^)(NSMutableArray *array))array
{
NSMutableArray *sourceArray = \
[[NSMutableArray alloc] initWithContentsOfFile:[self path:filePath]]; array(sourceArray); [sourceArray writeToFile:[self path:filePath]
atomically:YES];
} @end

Usage

获取沙盒中Documents目录下的一个文本文件YouXianMing.txt

判断这个沙盒中Documents目录下的一个文本文件YouXianMing.txt是否存在

判断沙盒中Documents目录下的一个文本文件YouXianMing.txt是否为一个文件

沙盒中Documents目录下的一个文本文件YouXianMing.txt的大小

读取/Library/Preferences目录下的一个字典类型的plist文件YouXianMing.plist

在沙盒根目录下创建文件夹层级结构/A/B/C

将根目录下的A文件夹以及子文件夹拷贝到Documents下的A文件夹当中

将集合(数组或者字典)写到/Library/Preferences下的tmp.plist当中

读取/Library/Preferences目录下的tmp.plist,修改其中的一个值并保存

没举例子的自己试一下就知道了.

so easy :) enjoy it.

iOS文件处理类的更多相关文章

  1. C++学习47 文件的概念 文件流类与文件流对象 文件的打开与关闭

    迄今为止,我们讨论的输入输出是以系统指定的标准设备(输入设备为键盘,输出设备为显示器)为对象的.在实际应用中,常以磁盘文件作为对象.即从磁盘文件读取数据,将数据输出到磁盘文件.磁盘是计算机的外部存储器 ...

  2. IOS文件操作的两种方式:NSFileManager操作和流操作

    1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...

  3. C++的IO处理中的头文件以及类理解(2)<sstream>头文件

    C++的IO处理中的头文件以及类理解(2)<sstream>头文件 头文件<sstream>中定义的类型都继承iostream头文件中定义的类型.除了继承得来的操作,sstre ...

  4. IOS上传图片方法类

    IOS上传图片方法类   iPhone开发中遇到上传图片问题,找到多资料,最终封装了一个类,请大家指点,代码如下 // // RequestPostUploadHelper.h // demodes ...

  5. IOS 文件夹结构

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/MyGameZone/article/details/24494765 IOS文件夹结构 说明 这些仅 ...

  6. php 文件日志类

    php文件日志类,按年月日组织目录结构. <?php class FileLog { private $_filepath; //文件路径 private $_filename; //日志文件名 ...

  7. C++文件流类与文件流对象

    文件流是以外存文件为输入输出对象的数据流.输出文件流是从内存流向外存文件的数据,输入文件流是从外存文件流向内存的数据.每一个文件流都有一个内存缓冲区与之对应. 请区分文件流与文件的概念,不用误以为文件 ...

  8. 使用Java的多线程和IO流写一个文件复制功能类

    创建一个复制功能类,继承Thread类,重写run()方法,把FileInputStream和FileOutputStream输入输出流写在run()方法内.示例代码如下: import java.i ...

  9. Code片段 : .properties属性文件操作工具类 & JSON工具类

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 j ...

随机推荐

  1. Angular 2 升级到 Angular 5

    Angular 2 升级到 Angular 5 ts文件最上面的import语句里不要添加 .ts 后缀 , 不然 npm start 编译会失败 . 虽然浏览器能打开项目的URL , 但是内容会丢失 ...

  2. Oracle PL/SQL编程之包(packages)

    1.简介 包用于在逻辑上组合过程和函数,它由包规范和包体组成. 我们可以使用create package来创建包,代码如下: ok,包创建完成,通过包的代码发现包的功能就是申明包中包含的过程和方法,红 ...

  3. ant如何编译项目

    Ant的概念 可能有些读者并不理解什么是Ant以及如何使用它,但只要使用通过Linux系统的读者,应该知道make这个命令.当编译Linux内核及一些软件的源程序时,经常要用这个命令.Make命令其实 ...

  4. 【ExtJS】FormPanel 布局(一)

    准备工作,布置一个最简单的Form,共5个组件,都为textfield. Ext.onReady(function(){ Ext.create('Ext.form.Panel', { width: 5 ...

  5. Log4J 配置文件模板及代码说明

    相对而言,这个日志系统的配置就没那么随意了,而且有些功能用起来也不是那么爽,譬如动态读取配置文件.不过鉴于使用这个日志的系统还是很多,所以也写一个demo贴出来,风格跟log4j2一样,配置的说明全在 ...

  6. Linux下模拟多线程的并发并发shell脚本

    分享一个在Linux下模拟多线程的并发脚本,使用这个脚本可以同时批量在定义数量的服务器上执行相关命令,比起普通for/while循环只能顺序一条一条执行的效率高非常多,在管理大批服务器时非常的实用.  ...

  7. [转]ASP.NET Web API基于OData的增删改查,以及处理实体间关系

    本文转自:http://www.cnblogs.com/darrenji/p/4926334.html 本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先 ...

  8. RabbitMQ - 远程过程调用

    试着用RabbitMQ进行RPC. 其实用RabbitMQ搞RPC也没什么特别的.只是我们需要在请求中再加入一个callback queue.比如这样: callbackQueueName = cha ...

  9. Sequence contains no elements : LINQ error

    1.错误意思: 出现错误的原因是:你要从一个null中取的数据. 2.错误的处理 1,使用FirstOrDefault() 来代替 First() 2.使用SingleOrDefault 来代替 Si ...

  10. CORS跨域请求C#版

    1.什么是跨域请求:  当从A网站使用AJAX请求B网站时,就会出现跨域请求. 此时B网站能够接收到A网站发来的请求并返回相应的结果,但是浏览器拿到B网站返回的数据时检测到与当前网站的域名不同,出于安 ...