IOS开发-文件管理(二)

五、Plist文件

String方式添加              

NSString *path = [NSHomeDirectory( )  stringByAppendingPathComponent:@"Array.plist"];

NSString *content = @"abcd";

[contect writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

Array方式添加        

NSString *path = [NSHomeDirectory( )  stringByAppendingPathComponent:@"Array.plist"];

[NSArray *array = [[NSArray alloc] initWithObjects:@"123", @"798",@"000",nil];       [array writeToFile:path atomically:YES];

Dictionary方式添加          

NSString *path = [NSHomeDirectory( )  stringByAppendingPathComponent:@"Dic.plist"];                        

NSDictionary *dic = [NSDictionary alloc] initWithObjects:@"first",@"second",@"third"forKeys:@"123",@"456",@"798"];                                                                       [dic writeToFile:path atomically:YES];

  • 数组、字典只能将BOOL、NSNumber、NSString、NSData、NSDate、NSArray、NSDictionary写入属性列表plist文件

六、读取文件类和常用方法

  • NSFileHandle类主要对文件内容进行读取和写入操作

  • NSFileManager类主要对文件的操作(删除、修改、移动、复制等等)

常用处理方法

+ (id)fileHandleForReadingAtPath:(NSString *)path  打开一个文件准备读取     

+ (id)fileHandleForWritingAtPath:(NSString *)path  打开一个文件准备写入

+ (id)fileHandleForUpdatingAtPath:(NSString *)path  打开一个文件准备更新

-  (NSData *)availableData; 从设备或通道返回可用的数据

-  (NSData *)readDataToEndOfFile; 从当前的节点读取到文件的末尾

-  (NSData *)readDataOfLength:(NSUInteger)length; 从当前节点开始读取指定的长度数据

-  (void)writeData:(NSData *)data; 写入数据

-  (unsigned long long)offsetInFile;  获取当前文件的偏移量

-  (void)seekToFileOffset:(unsigned long long)offset; 跳到指定文件的偏移量

-  (unsigned long long)seekToEndOfFile; 跳到文件末尾

-  (void)truncateFileAtOffset:(unsigned long long)offset; 将文件的长度设为offset字节

-  (void)closeFile;  关闭文件

向文件追加数据

NSString *homePath  = NSHomeDirectory( );        

NSString *sourcePath = [homePath stringByAppendingPathConmpone:@"testfile.text"];

NSFileHandle *fielHandle = [NSFileHandle fileHandleForUpdatingAtPath:sourcePath];

[fileHandle seekToEndOfFile];  将节点跳到文件的末尾

NSString *str = @"追加的数据"

NSData* stringData  = [str dataUsingEncoding:NSUTF8StringEncoding];

[fileHandle writeData:stringData]; 追加写入数据

[fileHandle closeFile];

定位数据                    

NSFileManager *fm = [NSFileManager defaultManager];

NSString *content = @"abcdef";

[fm createFileAtPath:path contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];

NSUInteger length = [fileHandle availabelData] length]; 获取数据长度

[fileHandle seekToFileOffset;length/2]; 偏移量文件的一半

NSData *data = [fileHandle readDataToEndOfFile];

[fileHandle closeFile];

复制文件                           

NSFileHandle *infile, *outfile; 输入文件、输出文件

NSData *buffer; 读取的缓冲数据

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *homePath = NSHomeDirectory( );

NSString *sourcePath = [homePath stringByAppendingPathComponent:@"testfile.txt"];  源文件路径

NSString *outPath = [homePath stringByAppendingPathComponent:@"outfile.txt"]; 输出文件路径

BOOL sucess  = [fileManager createFileAtPath:outPath contents:nil attributes:nil];

if (!success)

{

return N0;

}

infile = [NSFileHandle fileHandleForReadingAtPath:sourcePath]; 创建读取源路径文件

if (infile == nil)

{

return NO;

}

outfile = [NSFileHandle fileHandleForReadingAtPath:outPath]; 创建病打开要输出的文件

if (outfile == nil)

{

return NO;

}

[outfile truncateFileAtOffset:0]; 将输出文件的长度设为0

buffer = [infile readDataToEndOfFile];  读取数据

[outfile writeData:buffer];  写入输入

[infile closeFile];        关闭写入、输入文件

[outfile closeFile];

IOS 文件管理 2的更多相关文章

  1. IOS文件管理-NSFileMangager-NSdata

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

  2. ios文件管理

    <Application_Home>/AppName.app This is the bundle directory containing the applicationitself. ...

  3. iOS路径沙盒文件管理(转载)

    iOS路径沙盒文件管理,看到博主总结的很好,转载过来,原文:http://www.aichengxu.com/view/35264 一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文 ...

  4. IOS开发-文件管理(二)

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

  5. iOS开发-文件管理(一)

    iOS开发-文件管理(一) 一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.pli ...

  6. IOS 开发之文件管理

    一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.plist文件.sqlite数据库 ...

  7. iOS开发-文件管理

    iOS学习笔记(十七)--文件操作(NSFileManager) 浅析 RunLoop 解决EXC_BAD_ACCESS错误的一种方法--NSZombieEnabled iOS开发--Swift篇&a ...

  8. 【转】iOS开发-文件管理(一)

    iOS开发-文件管理(一) 一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.pli ...

  9. 文件管理中心iOS APP (国外市场:File Center) 技术支持

    文件管理中心iOS APP (国外市场:File Center) 技术支持网址:http://www.cnblogs.com/flychen/邮箱:592802944@qq.com

随机推荐

  1. 在NGINX作反向代理,CI(CodeIgniter)的PHP框架下限制管理目录的IP的实现

    这个搞得有点久,不过,还算完美解决. 主要是前端NGINX,后端也是NGINX. 前端的NGINX不好作相关的URL权限限制,因为所有的URL在CI里都要经过INDEX.PHP重定向. 并且,在后端N ...

  2. LeetCode_Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...

  3. 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证

    原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证 chsakell分享了前端使用AngularJS,后端使用ASP. ...

  4. 再论dynamic 关键字

    有关动态数据类型 ,大家估计在实际中用的比较多了,不是很陌生.有关自己在项目中 的实际钉子总结: 1  匿名对象中的字段,是只读的,不能赋值 2 动态类型 指向强类型实例,注意观察内部的属性可访问性 ...

  5. spring framework 4 源码阅读(1) --- 前期准备

    在开始看代码之前,需要做的第一件事是下载代码. 在这里:https://github.com/spring-projects/spring-framework 下载完成了发现使用gradle做的源代码 ...

  6. WebApi接口开发

    文档 规范的文档对接口的开发有着至关重要的作用,规范的文档能够使 双方对接口的定义以及接口的参数都有一个明确的概念,便于沟通和联调. 在接口的开发过程中,为了保证传递参数的传递的私密性,参数传输是需要 ...

  7. c语言结构体3之结构体嵌套

    注意: 1结构体内部再次定义一个结构体 但是没有创建结构体的实例  也就是说再次定义的结构体内部的变量会被当做母结构体的成员变量 struct tianchao { int data; ]; stru ...

  8. MyBatis初学者配置

    小配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC &qu ...

  9. (转载)XML Tutorial for iOS: How To Read and Write XML Documents with GDataXML

    In my recent post on How To Choose the Best XML Parser for Your iPhone Project, Saliom from the comm ...

  10. Qt Mac 下软件Release 公布dmg

    1.首先当然是用Qt Creator.编译一个Release版本号的软件 注意到编译出来的大小非常小,才420KB,由于一些类库还没包括进去的原因.如今还仅仅能在你本地执行,复制到其它Mac电脑就不能 ...