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. 遗传算法matlab实现

    我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 以下运用MATLAB实现遗传算法:   clc clear   %参数 a = 0 ; b = 4 ; e ...

  2. 一个ajax的后台controller

    @RequestMapping("/api/merBrand") @ResponseBody public ResultBrand merBrand(HttpServletRequ ...

  3. IOS 播放音频流媒体

    #pragma mark - 加载播放数据 - (void)loadData:(NSString *)musicUrl { NSURL *playURL = [NSURL URLWithString: ...

  4. #ifdef __cplusplus extern "C" {代码} 倒底是什么意思?

    时常在cpp的代码之中看到这样的代码: #ifdef __cplusplus   extern "C" { #endif //一段代码 #ifdef __cplusplus } # ...

  5. zabbix 对于logstash告警连续发邮件

    打上勾就行

  6. spoj1812-Longest Common Substring II(后缀自动机)

    Description A string is finite sequence of characters over a non-empty finite set Σ. In this problem ...

  7. URAL 1036

    题目大意:求前N位与后N位各个位和相等且总和等于S的2N位数的个数. KB     64bit IO Format:%I64d & %I64u 数据规模:1<=N<=50,0< ...

  8. C语言中所有变量和常量所使用的内存总结

    (1)相同点:三种获取内存的方法,都可以给程序提供可用内存,都可以用来定义变量给程序用.(2)不同点:栈内存对应C中的普通局部变量(别的变量还用不了栈,而且栈是自动的,由编译器和运行时环境共同来提供服 ...

  9. HTML--控制小人自由移动

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  10. Java IO :文件

    在java应用程序中,文件是一种常用的数据源或者存储数据的媒介.所以这一小节将会对Java中文件的使用做一个简短的概述.这里只提供一些必要的知识点. 通过Java IO读文件 如果你需要在不同端之间读 ...