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. 【转】Windows SDK入门浅谈

    前言 如果你是一个编程初学者,如果你刚刚结束C语言的课程.你可能会有点失望和怀疑:这就是C语言吗?靠它就能编出软件?无法想象Windows桌面上一个普通的窗口是怎样出现在眼前的.从C语言的上机作业到W ...

  2. 自制单片机之二-----AT89S51ISP下载线的制做

    最小系统板做好了,接下来就是做根ISP下载线了.否则程序怎么写到AT89S51芯片里呢? 先来认识一下AT89S51上ISP(在线编程)功能脚的定义 看上图的左边AT89S51引脚图的P1.5.P1. ...

  3. Android项目使用support v7时遇到的各种问题

    Android项目使用support v7时遇到的各种问题 点击你的工程右键-->Properties-->Android 1.查看你引用的appcompat_v7包是否引用正确 2.用较 ...

  4. Windows Azure 存储管理器 (2014)

     Windows Azure存储用户经常希望能够在"管理器"中查看他们的数据,管理器指的是一款可用于显示存储帐户数据的工具.我们之前提供了我们所知的存储管理器列表.在本文中,我 ...

  5. Android AutoCompleteTextView和MultiAutoCompleteTextView使用

    Android AutoCompleteTextView和MultiAutoCompleteTextView的功能类似于百度或者Google在搜索栏输入信息的时候,弹出的与输入信息接近的提示信息: 它 ...

  6. Jquery使用tbody编辑功能实现table输入计算功能

    实例:编写一个输入计算(被减数-减数=差). HTML: <body> <table> <thead> <tr> <td >被减数</ ...

  7. hdu4622-Reincarnation(后缀自动机)

    Problem Description Now you are back,and have a task to do:Given you a string s consist of lower-cas ...

  8. java_抽象类应用

    本例子通过一个实例来具体阐述抽象类的应用,首先一个抽象类Person2,里面定义了一些人的共有属性(年龄,姓名),和抽象方法want(),want()方法来具体实现不同的人的需求(学生想要成绩,工人想 ...

  9. git hub 资料汇总

    tobecrazy  Selenium automation test framework    https://github.com/tobecrazy/Demo Smartphone Test F ...

  10. cumber + selenium +java自动化测试

    1.新建一个maven项目,pom文件为: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&quo ...