博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FZEasyFile的使用
阅读量:6041 次
发布时间:2019-06-20

本文共 4849 字,大约阅读时间需要 16 分钟。

FZEasyFile的使用

 

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

以前你需要这么做才行:

NSFileManager *fileManager = [NSFileManager defaultManager];    //获取document路径,括号中属性为当前应用程序独享    NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *documentDirectory = [directoryPaths objectAtIndex:0];    //查找文件夹,如果不存在,就创建一个文件夹    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 
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 != 0)    {        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

 

 

 

 

 

 

转载地址:http://cdrhx.baihongyu.com/

你可能感兴趣的文章
基本调试命令 - u/ub/uf
查看>>
实现锁的多种方式和锁的高级用法
查看>>
C语言-一个fopen函数中未使用二进制模式(b)引发的血案
查看>>
Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
查看>>
Objective-C中常用的结构体NSRange,NSPoint,NSSize(CGSize),NSRect
查看>>
关于spark standalone模式下的executor问题
查看>>
TC SRM 664 div2 B BearPlaysDiv2 bfs
查看>>
Retrofit全攻略——基础篇
查看>>
代理模式
查看>>
具体问题具体分析
查看>>
【SqlServer系列】表达式(expression)
查看>>
maven与gradle的对比
查看>>
异常备忘:java.lang.UnsupportedClassVersionError: Bad version number in .class file
查看>>
uasy-datetimebox的使用
查看>>
Android Home键监听
查看>>
Java JVM虚拟机选项Xms/Xmx/PermSize/MaxPermSize(转)
查看>>
linux convert命令安装及使用
查看>>
JavaWeb(一)Servlet中乱码解决与转发和重定向的区别
查看>>
laravel5.5 Syntax error or access violation: 1071 Specified key was too long
查看>>
分布式锁与实现(一)——基于Redis实现
查看>>