Ios下的文件管理, Ios下不像windows 文件系统那样可以访问任何的文件目录,如C盘、D盘什么的。在Ios中每个应用程序只能访问当前程序的目录,也即sandbox(沙盒模型)。

iOS为每个应用程序分配一个目录,该目录下默认有四个文件目录:

app:我们程序开发压缩的包文件,包含里面的程序集和资源文件。

documents:应用程序数据存储在Documents 中,但机于NSUerDefaults的首选设置除外

Library:基于NSUserDefaults 的首选设置存储在在Library/perfercens文件夹中

tmp:存储临时文件。

在本次开发中我的任务是 写相关的错误日志并将其写入到本地文件中,因此我选择的路径是documents。

我用的是使用NSFileManager 类进行文件管理,NSData进行文件读取,NSMutableData 进行文件写入的方案进行实现的。

创建NSFileManager 类中碰到的问题倒不是很多,但是用NSData 进行文件读取时,我却碰到总是无法获取句柄的问题(也就无法获取内容信息)

一开始我用的方法: [NSData dataWithContentsOfFile:path] path为整个文件的全路径,但是不论我怎么尝试,还是无法获取里面的内容。

后来我结合NSFileManager 来进行读取数据(在这里文件都是已经存在的)步骤如下:

例如:我们要读取的文件为:documents/cordova/log/201405.log

1、需要通过[NSFileManager changeCurrentDircectoryPath:[filePath stringByExpandingTildeInPath]]; 方法切换到documents/cordova/log  目录下面。(filePath 就是指定的该全部目录 具体可以参考NSSearchPathForDirectoriesInDomains 方法)

2、然后使用NSData *reader= [NSData dataWithContentsOfFile:@"201405.log"]  方法直接读取当前文件夹下的文件数据,这里已经将数据读取到内存中。

3、最后追加内容写入,

NSMutableData *write=[[NSMutableData alloc] initWithData:reader];--NSdata 初始化

[write appendData:meage] --添加数据

[write writeToFile:@“201405.log” atomically:YES];

其实我也不明白为什么直接给NSData全路径会出现得不到数据的问题。还有听说很多人说NSUrl 方式来写入和读取数据比较好。 下次若有时间也去好好体验一次。。。。这里先保留了!

 二、文件日志 读写文件

//读写操作

+(void)Info:(NSString *)message

{

@synchronized(self){//  读写线程安全

// DBNAME 是要查找的文件名字,文件全名

NSString *filePath = [self getLogBasePath];

NSFileManager *fileManager = [NSFileManager defaultManager];

BOOL isDirtory=NO;

if (!([fileManager fileExistsAtPath:filePath isDirectory:&isDirtory]&&isDirtory)) {

//若不存在文件夹则创建文件夹

[fileManager createDirectoryAtPath:filePath withIntermediateDirectories:NO attributes:nil error:nil];

}

NSString *date=  [[NSDate date] ToStringWithFormmate:@"yyyyMMdd"];

NSString *dateLogName=[date stringByAppendingString:@".txt"];

NSString *dateFilePath= [filePath stringByAppendingPathComponent:dateLogName];

if (![fileManager fileExistsAtPath:dateFilePath]) {

//文件不存在则创建

if(![fileManager createFileAtPath:dateFilePath contents:nil attributes:nil])return;

}

NSString *time=[[NSDate date] ToStringWithFormmate:@"yyyy-MM-dd hh:mm:ss"];

NSString *logMessage=[[NSString alloc] initWithFormat:@"%@   %@ \n",time,message];

NSData *reader= [NSData dataWithContentsOfFile:dateFilePath];

NSMutableData *write=[[NSMutableData alloc] initWithData:reader];//NSdata 初始化

[write appendData:[logMessage dataUsingEncoding:NSUTF8StringEncoding]]; //添加数据

BOOL reslut= [write writeToFile:dateFilePath atomically:YES];

if (reslut) {

NSLog(@"success");

}else

{

NSLog(@"fail");

}

}

}

日志读取

+(NSString*) readFileWithName:(NSString*)name

{

NSString *filePath = [self getLogBasePath];

NSData *reader= [NSData dataWithContentsOfFile:[filePath stringByAppendingPathComponent:name]];

NSString *result=[[NSString alloc] initWithData:reader encoding:NSUTF8StringEncoding];

return result;

}

+(NSString*)getLogBasePath //公用方法获取指定的目录

{

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

NSString *documentDirectory = [directoryPaths objectAtIndex:0];

// DBNAME 是要查找的文件名字,文件全名

NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"log"];

return filePath;

}

IOS文件管理-NSFileMangager-NSdata的更多相关文章

  1. iOS 基础类解析 - NSData、NSMutableData

    iOS 基础类解析 - NSData.NSMutableData 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致& ...

  2. IOS 文件管理 2

    IOS开发-文件管理(二) 五.Plist文件 String方式添加               NSString *path = [NSHomeDirectory( )  stringByAppen ...

  3. iOS NSDictionary、NSData、JSON数据类型相互转换

    iOS经常需要用到数据类型的转换,下面列举一下常用类型的转换. 1.NSDictionary类型转换为NSData类型: //NSDictionary -> NSData: NSDictiona ...

  4. iOS 图片转NSData-b

    iOS开发中 UIImage可能经常需要转为NSData 上传 传递等等 有两个比较常用的方法 UIImageJPEGRepresentation UIImagePNGRepresentation 第 ...

  5. iOS NSString 和NSData 转换

    NSString 转换成NSData 对象 NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncodin ...

  6. iOS NSDictionary、NSData、JSON等 数据类型相互转换

    1.NSDictionary类型转换为NSData类型: NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @&qu ...

  7. iOS - Usage of NSData

    Reference link : https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/BinaryData/T ...

  8. <iOS>UIImage变为NSData并进行压缩

    http://www.cnblogs.com/robinkey/archive/2013/01/21/2869930.html //sdk中提供了方法可以直接调用 UIImage *img = [UI ...

  9. ios开发之 -- NSData 和 NSString , UIImage 等之间的互转

    //NSData转换为UIImage NSData *imageData = [NSData dataWithContentsOfFile: imagePath]; UIImage *image = ...

随机推荐

  1. mysql数据库回滚

    在应用$mysqli时,因没常用到数据回滚,老忘,整理下,做个记录. $mysqli->autocommit(FALSE);//自动提交设置关闭 $mysqli->query(" ...

  2. 决策树原理、Scikit-learn实现及其在生物信息中的应用

    之前转过一篇文章:2016年GitHub排名前20的Python机器学习开源项目(转),说明现在已经有了很多很好的机器学习的包,我们不必从底层开始实现,只要懂点算法.会看文档,一般人也能玩好机器学习. ...

  3. 用java实现冒泡排序法

    一.基本思路: 冒泡排序是一种简单的交换类排序.其基本思路是,从头开始扫描待排序的元素,在扫描过程中依次对相邻元素进行比较,将关键字值大的元素后移.每经过一趟排序后,关键字值最大的元素将移到末尾,此时 ...

  4. [BI基础] 一些不得不了解的概念

    0.Hadoop hadoop主要是用来对海量数据进行存储和计算的. 它本身是一个分布式系统,核心由分布式文件系统hdfs,和分布式计算框架mapreduce组成,在存储和计算时能够发挥出集群中每台机 ...

  5. (17)odoo方法和修饰器

    ---------------------更新时间:11:06 2016-09-27 星期二18:06 2016-09-18 星期日10:31 2016-03-01 星期二-------------- ...

  6. PHP读取文件夹目录,按时间排序,大小排序,名字排序

    工作中有时候会遇到文件存储数据,但是在前台显示的时候又因为没有数据库,无法使用上传或最后一次修改日期字段排序,所以有了如下代码: <?php $dir = "./";//目录 ...

  7. Subsets [LeetCode]

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  8. Html5新标签解释及用法

    Html5新标签解释及用法 HTML 5 是一个新的网络标准,目标在于取代现有的 HTML 4.01, XHTML 1.0 and DOM Level 2 HTML 标准.它希望能够减少浏览器对于需要 ...

  9. JavaScript中的test()方法

    定义和用法 test() 方法用于检测一个字符串是否匹配某个模式. 语法 RegExpObject.test(string) 参数 描述 string 必需.要检测的字符串. 返回值 如果字符串 st ...

  10. PHP Mongodb 基本操作

    <?php include "config/config.inc.php";$host = $config['host'];$port = $config['port']; ...