FZEasyFile的使用

简介:

FZEasyFile的使用

https://github.com/jiecao-fm/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 <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 != 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




目录
相关文章
|
机器学习/深度学习 并行计算 PyTorch
PyTorch安装教程
PyTorch是学习深度学习时常用的Python神经网络框架,本文将介绍其部分版本的安装方式。Windows和Linux通用。 作者使用anaconda作为管理虚拟环境的工具。以下工作都在虚拟环境中进行,对Python和Aanaconda的安装及对虚拟环境的管理本文不作赘述,后期可能会撰写相关的博文。
PyTorch安装教程
|
10月前
|
存储 Java 开发工具
在Eclipse配置安装Android详细教程(新手)
在Eclipse配置安装Android详细教程(新手)
211 1
|
9月前
|
安全 网络安全 API
kotlin安卓开发JetPack Compose 如何使用webview 打开网页时给webview注入cookie
在Jetpack Compose中使用WebView需借助AndroidView。要注入Cookie,首先在`build.gradle`添加WebView依赖,如`androidx.webkit:webkit:1.4.0`。接着创建自定义`ComposableWebView`,通过`CookieManager`设置接受第三方Cookie并注入Cookie字符串。最后在Compose界面使用这个自定义组件加载URL。注意Android 9及以上版本可能需要在网络安全配置中允许第三方Cookie。
|
10月前
|
存储 编解码 语音技术
使用智能媒体生产ICE剪辑OSS视频文件
本篇介绍智能媒体生产ICE一些常见场景,如裁剪、拼接、字幕、ASR等,通过一些时间线示例,介绍如何快速剪辑OSS上的视频文件。
414 0
使用智能媒体生产ICE剪辑OSS视频文件
|
10月前
|
存储 运维 监控
基于Serverless搭建批量打马赛克服务
本方案实现在阿里云Serverless函数计算服务中搭建图片批量打马赛克服务,具备自动将用户上传到OSS桶内的图片批量打上马赛克功能,实现用户敏感信息自动化处理。
217 0
基于Serverless搭建批量打马赛克服务
|
10月前
|
Dubbo Java 应用服务中间件
微服务治理热门技术揭秘:无损上线
本文一一介绍了微服务治理的热门技术无损上线的方案与实现的细节。
微服务治理热门技术揭秘:无损上线
|
10月前
|
云安全 安全 网络安全
【看案例】完美日记:保障电商大促活动平稳运行
完美日记在“双11”电商大促、“双12”购物节期间,依靠阿里云DDoS高防IP、云安全中心、Web应用防火墙基础安全三件套,从容面对业务高峰、安全压力,提升大促活动期间系统的快速响应能力和安全性。
224 0
|
10月前
|
弹性计算 小程序 数据建模
快速完成LNMP环境搭建、域名注册、SSL证书申请
本文主要分享如何利用云服务器进行项目后端的开发,向大家分享一些踩坑的经验。
146 0
快速完成LNMP环境搭建、域名注册、SSL证书申请
|
10月前
|
存储 NoSQL 关系型数据库
基于Tablestore 实现海量订单日志数据存储
从最早的互联网高速发展、到移动互联网的爆发式增长,再到今天的产业互联网、物联网的快速崛起,各种各样新应用、新系统产生了众多订单类型的需求,比如电商购物订单、银行流水、运营商话费账单、外卖订单、设备信息等,产生的数据种类和数据量越来越多;其中订单系统就是一个非常广泛、通用的系统。而随着数据规模的快速增长、大数据技术的发展、运营水平的不断提高,包括数据消费的能力要求越来越高,这对支撑订单系统的数据库设计、存储系统也提出了更多的要求。在新的需求下,传统的经典架构面临着诸多挑战,需要进一步思考架构优化,以更好支撑业务发展。
336 0
基于Tablestore 实现海量订单日志数据存储