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. ImportError: libSM.so.6: cannot open shared object file: No such file or directory

    Solution sudo apt-get install libsm6 Similarly ImportError: libXrender.so.1: cannot open shared obje ...

  2. C语言中malloc函数与free函数

    - malloc函数 全称是memory allocation,中文叫动态内存分配,用于申请一块连续的.指定大小的内存块区域以void*类型返回分配的内存区域地址,当无法知道内存具体位置的时候,想要绑 ...

  3. PHP如何判断对象为空的方法分享

    1.网上很多方法分享说用empty()方法判断对象是否为空.empty()的方法介绍:格式:bool empty ( mixed var )功能:检查一个变量是否为空返回值:若变量不存在则返回 TRU ...

  4. Word 只读模式修改

    一.设置只读模式: 方法一: 打开目标文件,点击另存为,如下: 2.点击常规选项,勾选‘建议以只读方式打开文档,也可以在此页给文档加密: 方法二: 在工具栏-->审阅--->限制编辑: 二 ...

  5. 分析apache日志,统计访问量

    cat nondomain_access_log.20090722 |awk '{print $1}'| sort | uniq -c |sort -nr

  6. GuavaCache实现堆缓存

    一.缓存 频繁地数据库I/O是很大的负担,而缓存是减少I/O提高性能的银弹. 缓存的实现一般上是以类似map的键值对的形式,并对外提供put和get方法,但缓存空间不能无限扩大,所以它得有一种缓存清除 ...

  7. JavaScript AMD 与CMD的代码区别

    1:CMD 依赖就近 define(function(require,export) { var b =1; var a = require("../a"); a.dosometh ...

  8. java.io.FileNotFoundException

    1. 概述 上传图片到 o:/mmfiles_2017/39973/25444/333.jpg目录下 目录不存在会报标题中的错误 必须先创建目录 2. 方法 File filePath = new F ...

  9. SPOJ QTREE6

    题意 给你一棵\(n\)个点的树,编号\(1\)~\(n\).每个点可以是黑色,可以是白色.初始时所有点都是黑色.有两种操作 \(0\ u\):询问有多少个节点\(v\)满足路径\(u\)到\(v\) ...

  10. 洛谷P2196 挖地雷(dp)

    题意 题目链接 Sol 早年NOIP的题锅好多啊.. 这题连有向边还是无向边都没说(害的我wa了一遍) 直接\(f[i]\)表示到第\(i\)个点的贡献 转移的时候枚举从哪个点转移而来 然后我就用一个 ...