FZEasyFile的使用

https://github.com/jiecao-fm/FZEasyFile

操作沙盒文件很恶心,但用上FZEasyFile就变得简单了.

以前你需要这么做才行:

NSFileManager *fileManager = [NSFileManager defaultManager];

    //获取document路径,括号中属性为当前应用程序独享
NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [directoryPaths objectAtIndex:]; //查找文件夹,如果不存在,就创建一个文件夹
NSString *dir = [documentDirectory stringByAppendingPathComponent:@SAVEDIR];
NSLog(@"cache dir %@", dir);
if(![fileManager fileExistsAtPath:dir])
{
if(![fileManager createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:nil])
{
NSLog(@"create dir:(%@) error", dir);
return;
}
} //定义记录文件全名以及路径的字符串filePath
NSString *filePath = [dir stringByAppendingPathComponent:[[NSString alloc]initWithFormat:@"/%@", filename]]; //查找文件,如果不存在,就创建一个文件
NSData *data = [lHtml dataUsingEncoding:NSUTF8StringEncoding];
if (![fileManager fileExistsAtPath:filePath]) {
[fileManager createFileAtPath:filePath contents:data attributes:nil];
}

现在你只需要这么做:

FZEasyFile *easyFile = [FZEasyFile sharedInstance];
[easyFile createFile:fileName overwrite:NO];
NSOutputStream *output = [NSOutputStream outputStreamToFileAtPath:[easyFile fullFileName:fileName] append:NO];

so easy :)

附录:

以下为我自己修改的版本,感谢原作者的分享精神!

EasyFile.h

//
// EasyFile.h
// EasyFile
//
// Copyright (c) 2014年 zhou jun All rights reserved.
// #import <Foundation/Foundation.h> typedef enum
{ /*
/Documents
/Library/Caches
/Library/Preferences
/tmp
*/ DOCUMENTS = 0x99,
CACHES,
PREFERENCES,
TMP, } EFolderFlag; @interface EasyFile : NSObject /**
convert the short file name to full file name. e.g. "mycache/user/icon.png" -> "/Users/zhoujun/Library/Application Support/iPhone Simulator/7.1/Applications/ABCE2119-E864-4492-A3A9-A238ADA74BE5/Documents/mycache/user/icon.png".
@return full file name.
*/
+ (NSString *)fullFileName:(NSString *)shortFileName folderType:(EFolderFlag)type; /**
create a file
@param fileName fileName file path and file name, e.g. "mycache/user/icon.png".
@param shouldOverwrite YES:if the file exists then overwirte it, NO:if the file exists then do nothing
*/
+ (void)createFile:(NSString *)fileName overwrite:(BOOL)flag folderType:(EFolderFlag)type; /**
test if the file exists.
@param fileName file path and file name, e.g. "mycache/user/icon.png".
@return YES if exists, NO otherwise.
*/
+ (BOOL)isFileExists:(NSString *)fileName folderType:(EFolderFlag)type; @end

EasyFile.m

//
// EasyFile.m
// EasyFile
//
// Copyright (c) 2014年 zhou jun All rights reserved.
// #import "EasyFile.h" @implementation EasyFile + (NSString *)fullFileName:(NSString *)shortFileName folderType:(EFolderFlag)type
{
NSString *rootPath = NSHomeDirectory(); switch (type)
{
case DOCUMENTS:
rootPath = [NSHomeDirectory() stringByAppendingString:@"/Documents"];
break; case CACHES:
rootPath = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"];
break; case PREFERENCES:
rootPath = [NSHomeDirectory() stringByAppendingString:@"/Library/Preferences"];
break; case TMP:
rootPath = [NSHomeDirectory() stringByAppendingString:@"/tmp"];
break; default:
rootPath = [NSHomeDirectory() stringByAppendingString:@"/Documents"];
break;
} NSString *file = [rootPath stringByAppendingPathComponent:shortFileName]; return file;
} + (void)createFile:(NSString *)fileName overwrite:(BOOL)flag folderType:(EFolderFlag)type
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSRange lastTag = [fileName rangeOfString:@"/" options:NSBackwardsSearch]; if (lastTag.location != NSNotFound && lastTag.location != )
{
NSString *shortDir = [fileName substringToIndex:lastTag.location];
NSString *fullDir = [self fullFileName:shortDir folderType:type]; if (![fileManager fileExistsAtPath:fullDir])
{
[fileManager createDirectoryAtPath:fullDir
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
} NSString *file = [self fullFileName:fileName folderType:type]; if (flag || ![fileManager fileExistsAtPath:file])
{
BOOL suc = [fileManager createFileAtPath:file contents:nil attributes:nil];
NSLog(@"create file(%@) %@", file, suc ? @"successfully" : @"failed");
}
} + (BOOL)isFileExists:(NSString *)fileName folderType:(EFolderFlag)type
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *file = [self fullFileName:fileName folderType:type];
return [fileManager fileExistsAtPath:file];
} @end

FZEasyFile的使用的更多相关文章

  1. iOS 利用FZEasyFile本地保存 和 常规保存

    1.常规保存(较麻烦) NSFileManager *fileManager = [NSFileManager defaultManager]; //获取document路径,括号中属性为当前应用程序 ...

随机推荐

  1. Oracle DMP

    通过DMP对Oracle数据库进行导入导出 打开“开始”--->输入cmd,打开cmd命令窗口,输入以下命令即可 1导出 (1)将数据库ORACLE完全导出,用户名system密码manager ...

  2. springmvc的json数据交互

    准备 @RequestBody 作用: @RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内容(json ...

  3. 基于steamworks获取steam用户头像

    查看官网api,使用c++写的,转成c#之后,有个问题就是,图片显示问题 我们可以获取到一个含有图片信息的byte[] 然后 private Texture2D downloadedAvatar; p ...

  4. Node.js中http-server的使用

    Node.js中http-server的使用 使用阿里的npm镜像 国外的npm太慢了.查看一下自己使用的源: npm config get registry 应该显示https://registry ...

  5. OAuth2.0 微信授权机制

    我在了解设计Restful接口的时候,发现涉及到接口验证,可以利用OAuth2.0机制来验证. 我开发的微信端Web网页通过微信授权的时候,微信端也是用OAuth2.0机制来获取用户基本信息. OAu ...

  6. Golang教程:并发介绍

    Go是并发语言,而不是并行语言.在讨论Go并发性之前,我们必须首先了解什么是并发,以及并发与并行的区别. 什么是并发 并发(concurrency)是指一次处理大量事情的能力.让我们用一个例子来说明. ...

  7. [转]象棋AI算法(二)

    本文转自:http://blog.csdn.net/u012723995/article/details/47143569 参考文献:http://bbs.blueidea.com/thread-30 ...

  8. TSM简介

    转自:https://www.cnblogs.com/Study-Blog/p/8644376.html Tivoli TSM产品功能详述Tivoli TSM(Tivoli Storage Manag ...

  9. The Internet Communications Engine (Ice) 跨平台异构通讯方案 第二弹-Hello world!

    如果不知道ICE是什么的同学,请看上一篇的ICE简介:http://www.cnblogs.com/winds/p/3864677.html 好了,HelloWorld,从中间语言讲起. 首先,我们新 ...

  10. Ruby 踩坑 “Failed to build gem native extension”

    ruby新手,总是会出现这样那样的问题,这里先记录下,希望能解决你得问题. 首先是安装ruby 环境,楼主愚钝,在公司和自己的电脑上来来回回整了好几天,每次安装 gem 包的时候总是报错,错误信息大致 ...