FZEasyFile的使用
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的使用的更多相关文章
- iOS 利用FZEasyFile本地保存 和 常规保存
1.常规保存(较麻烦) NSFileManager *fileManager = [NSFileManager defaultManager]; //获取document路径,括号中属性为当前应用程序 ...
随机推荐
- CentOS7 wget 安装Redis,开机启动配置
安装 GCC yum install gcc yum install tcl 安装wget yum install wget 1. 下载Redis wget http://download.red ...
- OpenGL6-纹理动画
代码下载 #include "CELLWinApp.hpp"#include <gl/GLU.h>#include <assert.h>#include & ...
- vim实战:插件安装(Vundle,NerdTree)
一:插件管理器Vundle 1.简介 Vundle是vim的一个插件管理器, 同时它本身也是vim的一个插件.插件管理器用于方便.快速的安装.删除.Vim更新插件.vim Vundle插件官方地址:h ...
- 【ExtJS】关于Component生命周期
很久以前就学习过extjs的组件生命周期,很久之后,再回头看一看,又增加好多新的认识. extjs组件生命周期大体分为3个阶段:初始化.渲染.销毁. 第一阶段:初始化 初始化工作开始于组件的诞生,所有 ...
- 在unity中使用protobuf
https://www.jianshu.com/p/b135676dbe8d 上面的提供序列化和常见的序列化有点不一样 下面的是常见的序列化的,但是他没有讲怎么用工具生成.proto--->.c ...
- MacOS系统下的图形化工具
MacOS系统下的图形化工具 MacOS系统下安装了Git后,发现如果Git中有中文文档操作还是比较麻烦(需要输入中文的文件名).图形化对Git的操作还是相对于方便一些.所以准备找一个图形化的工具. ...
- jq访问网络接口实例
最近需要在app生活频道上,需要添加一些类目,这就需要用到一些公用的开放接口,ajax其实调用并不复杂,但是结合jquery则显得更简洁一些,下面一起来看看jquery调用后台api. 代码如下: & ...
- Why Isn't curr_items Decreasing When Items Expire?
Why Isn't curr_items Decreasing When Items Expire?
- Make sure that the controller has a parameterless public constructor.
An error occurred when trying to create a controller of type 'CCD.Web.Controllers.TWAccountControlle ...
- nodejs图片裁剪、缩放、水印
关于nodejs下图片的裁剪.水印,网上的模块很多,主要如下: gm:https://github.com/aheckmann/gm node-canvas:https://github.com/Au ...