1、常规保存(较麻烦)

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];
} 2、FZEasyFile保存
导入的文件:

//

//  FZEasyFile.h

//  FZEasyFile

//

//  Created by zhou jun on 14-4-29.

//  Copyright (c) 2014年 shannonchou. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface FZEasyFile : NSObject

/**

singleton pattern.

@return the shared instance.

*/

+ (FZEasyFile *)sharedInstance;

/**

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;

/**

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;

/**

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)shouldOverwrite;

@end

//

//  FZEasyFile.m

//  FZEasyFile

//

//  Created by zhou jun on 14-4-29.

//  Copyright (c) 2014年 shannonchou. All rights reserved.

//

#import "FZEasyFile.h"

@implementation FZEasyFile

static FZEasyFile *instance;

+ (FZEasyFile *)sharedInstance {

if (!instance) {

@synchronized (FZEasyFile.class){

if (!instance) {

instance = [[FZEasyFile alloc] init];

}

}

}

return instance;

}

- (NSString *)fullFileName:(NSString *)shortFileName {

//search the "document" path

NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDirectory = [directoryPaths objectAtIndex:0];

NSString *file = [documentDirectory stringByAppendingPathComponent:shortFileName];

return file;

}

- (BOOL)isFileExists:(NSString *)fileName {

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *file = [self fullFileName:fileName];

return [fileManager fileExistsAtPath:file];

}

- (void)createFile:(NSString *)fileName overwrite:(BOOL)shouldOverwrite {

NSFileManager *fileManager = [NSFileManager defaultManager];

//create file directory, include multilayer directory

NSRange lastTag = [fileName rangeOfString:@"/" options:NSBackwardsSearch];

if (lastTag.location != NSNotFound && lastTag.location != 0) {

NSString *shortDir = [fileName substringToIndex:lastTag.location];

NSString *fullDir = [self fullFileName:shortDir];

NSLog(@"full directory: %@", fullDir);

if (![fileManager fileExistsAtPath:fullDir]) {

[fileManager createDirectoryAtPath:fullDir withIntermediateDirectories:YES attributes:nil error:nil];

}

}

NSString *file = [self fullFileName:fileName];

NSLog(@"full file name:%@", file);

//file not exists or want to overwrite it

if (shouldOverwrite || ![fileManager fileExistsAtPath:file]) {

BOOL suc = [fileManager createFileAtPath:file contents:nil attributes:nil];

NSLog(@"create file(%@) %@", file, suc ? @"successfully" : @"failed");

}

}

@end

使用方法:

导入头文件:


#import "FZEasyFile.h"

判断地址是否已存在


[EasyFile isFileExists:@"my/file/path/info.txt"]

创建


[EasyFile createFile:"my/file/path/info.txt" overwrite:NO];

写入文件中:拼接和覆盖写入


[FZEasyFile writeFile:"my/file/path/info.txt" contents:[@"a" dataUsingEncoding:NSUTF8StringEncoding] append:NO];
[FZEasyFile writeFile:"my/file/path/info.txt" contents:[@"b" dataUsingEncoding:NSUTF8StringEncoding] append:YES];

After these calling the content of the file is "ab".


删除文件


[FZEasyFile removeFile:"my/file/path/info.txt"];
[FZEasyFile removeFile:"my/file/path"];

获取绝对路径(实际存储路径)


NSString *fullName = [EasyFile fullFileName:"my/file/path/info.txt"];

转换

After getting the full name, you can pass it to other API, such as NSInputStream:


NSInputStream *input = [NSInputStream inputStreamWithFileAtPath:fullName];

 

iOS 利用FZEasyFile本地保存 和 常规保存的更多相关文章

  1. ASP.NET下载远程图片保存到本地的方法、保存抓取远程图片

    以下介绍两种方法:1.利用WebRequest,WebResponse 类 WebRequest wreq=WebRequest.Create("http://www.xueit.com/e ...

  2. Ruby1.9.3-下载网络图片至本地,并按编号保存。

    #本程序功能:下载网络图片至本地,并按编号保存. #使用Ruby1.9.3在winxp_sp3下编写. require 'nokogiri' require 'open-uri' #以下 根据网址解析 ...

  3. iOS五种本地缓存数据方式

    iOS五种本地缓存数据方式   iOS本地缓存数据方式有五种:前言 1.直接写文件方式:可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据 ...

  4. Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面

    Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面 关于欢迎界面 很多App第一次启动都会有一个欢迎界面,欢迎界面往往决定这用户对App的第一映像,所以欢 ...

  5. 详细的图文介绍如何利用XAMPP本地建站的环境配置教程

    原文:详细的图文介绍如何利用XAMPP本地建站的环境配置教程 WordPress 是一个简便快捷,用途广,人气旺的一个开源的博客建站程序.很有很多等您去发现. 简便快捷:在性能上易于操作.易于浏览: ...

  6. UIView封装动画--iOS利用系统提供方法来做关键帧动画

    iOS利用系统提供方法来做关键帧动画 ios7以后才有用. /*关键帧动画 options:UIViewKeyframeAnimationOptions类型 */ [UIView animateKey ...

  7. UIView封装动画--iOS 利用系统提供方法来做弹性运动

    iOS 利用系统提供方法来做弹性运动 /*创建弹性动画 damping:阻尼,范围0-1,阻尼越接近于0,弹性效果越明显 velocity:弹性复位的速度 */ [UIView animateWith ...

  8. iOS利用Application Loader打包提交到App Store时遇到错误The filename 未命名.ipa in the package contains an invalid character(s). The valid characters are:A-Z ,a-z,0-9,dash,period,underscore,but the name cannot start w

    iOS利用Application Loader打包提交到App Store时遇到错误: The filename 未命名.ipa in the package contains an invalid ...

  9. UIView封装动画--iOS利用系统提供方法来做转场动画

    UIView封装动画--iOS利用系统提供方法来做转场动画 UIViewAnimationOptions option; if (isNext) { option=UIViewAnimationOpt ...

随机推荐

  1. (入门整理学习一)Asp.net core

    1.安装.net code SDK,vs Code;vs code c#插件可在软件扩展 (我网盘有)  vs2015上安装教程:http://www.cnblogs.com/wangrudong00 ...

  2. jmeter--函数助手对话框之参数详解

    详解JMeter函数和变量 测试人员可以在JMeter的选项菜单中找到函数助手对话框("Function Helper"对话框),如图11-1所示. 图11-1 函数助手(Func ...

  3. 大话Spark(8)-源码之DAGScheduler

    DAGScheduler的主要作用有2个: 一.把job划分成多个Stage(Stage内部并行运行,整个作业按照Stage的顺序依次执行) 二.提交任务 以下分别介绍下DAGScheduler是如何 ...

  4. 再谈ITFriend网站的定位

    在网站开发阶段.内部测试阶段.公开测试阶段,让诸多好友和网友,参与了我们的网站ITFriend的体验和测试.其中,大家非常关心,我们的网站是干什么的.在我们不做任何解释的情况下,有的网站认为ITFri ...

  5. 【Codeforces Round #435 (Div. 2) B】Mahmoud and Ehab and the bipartiteness

    [链接]h在这里写链接 [题意] 让你在一棵树上,加入尽可能多的边. 使得这棵树依然是一张二分图. [题解] 让每个节点的度数,都变成二分图的对方集合中的点的个数就好. [错的次数] 0 [反思] 在 ...

  6. [java面试]宇信易诚 广州分公司 java笔试题目回顾录

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/28479895 作者:sushengmiyan -------------------- ...

  7. tensorflow 的 Batch Normalization 实现(tf.nn.moments、tf.nn.batch_normalization)

    tensorflow 在实现 Batch Normalization(各个网络层输出的归一化)时,主要用到以下两个 api: tf.nn.moments(x, axes, name=None, kee ...

  8. MongoDB集群安装与配置2.4.3版本

    mongoDB安装http://www.mongodb.org/downloads拷文件到# cp mongodb-linux-i686-2.4.1.tgz /usr/local/进入目录:# cd ...

  9. hdu 4406 费用流

    这题问题就是当前时刻究竟选择哪门课程,易知选择是和分数有关的,而且是一个变化的权值,所以能够用拆点的方式,把从基础分到100分都拆成点.但若这样拆点的话,跑费用流时就必须保证顺序.这样就麻烦了..观察 ...

  10. 如何在移动web模仿客户端给input输入框添加自定义清除按钮

    项目有个需求就是在input输入框添加清除按钮,网上查找资料加上自己琢磨终于弄出来了. 灵感来自于 http://www.zhangxinxu.com/wordpress/?p=4077 由于项目已经 ...