一、NSFileManager:
1.1、获取NSFileManager
NSFileManager *manager = [NSFileManager defaultManager];     NSFileManager是单例模式,所以不能使用alloc+init创建  
 

1.2、文件操作

NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];  //创建路径
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];   编码数据
BOOL sucess = [manager createFileAtPath:filePath contents:data attributes:nil]; 创建文件,参数:文件路径、文件内容、文件的属性
NSData *datas = [manager contentsAtPath:filePath];  根据路径读取文件
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];   将文件数据转换成string
BOOL sucess2 = [manager moveItemAtPath:filePath toPath:targetPath error:nil]  移动文件,移动的同时可以重命名文件名
BOOL sucess3 = [manager copyItemAtPath:filePath toPath:targetPath error:nil]; 复制文件
BOOL isExist = [manager fileExistsAtPath:filePath];//判断文件是否存在  
BOOL sucess4 = [manager removeItemAtPath:filePath error:nil];   删除文件
NSDictionary *dic = [manager attributesOfItemAtPath:filePath error:nil];  获取文件属性
BOOL success = [manager changeFileAttributes:attr atPath:path]                        更改文件的属性 
BOOL success = [manager isReadablefileAtPath:path] 测试文件是否存在,且是否能执行读操作 
BOOL success = [manager isWritablefileAtPath:path] 测试文件是否存在,且是否能执行写操作
 

1.3、目录操作

BOOL success1 = [manager createDirectoryAtPath:filePaths withIntermediateDirectories:YES attributes:nil error:&error]; 创建文件夹
-(NSString *)currentDirectoryPath                      获取当前目录 
-(BOOL)changeCurrentDirectoryPath:path                更改当前目录 
-(BOOL)copyPath:from toPath:to handler:handler      复制目录结构,to不能已经存在 
-(BOOL)createDirectoryAtPath:path attributes:attr    创建目录 
-(BOOL)fileExistsAtPath:path isDirectory:(BOOL *)flag       测试文件是否为目录 (flag存储结构yes/no) 
-(NSArray *)contentsOfDirectoryAtPath:path              列出目录的内容 
-(NSDirectoryEnumerator *)enumeratorAtPath:path  枚举目录的内容 
-(BOOL)removeFileAtPath:path handler:handler    删除空目录 
-(BOOL)movePath:from toPath:to handler:handler    重命名或移动一个目录,to不能是已经存在的 
 

二、NSFileHandle:

2.1 获取NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];    获取文件准备写入
                                   (NSFileHandle *)fileHandleForReadingAtPath:path                        打开一个文件准备读取
                                   (NSFileHandle *)fileHandleForUpdatingAtPath:path                        打开一个文件准备更新(读取和写入)
FileSize = [[attributes objectForKey:NSFileSize] intValue]    获取文件大小
[handle seekToEndOfFile]; 将游标链接到文件尾部
[handle writeData:data];    在尾部继续写入数据
[handle closeFile];   关闭文件
 
-(NSData *)readDataToEndOfFile                                            读取其余的数据直到文件末尾(最多UINT_MAX字节)
-(NSData *)readDataOfLength:(unsigned int)bytes 从文件读取指定数目bytes的内容
 
-(unsigned long long) offsetInFile      获取当前文件的偏移量
-(void)seekToFileOffset:offset         设置当前文件的偏移量 
-(unsigned long long) seekToEndOfFile      将当前文件的偏移量定位的文件末尾
-(void)truncateFileAtOffset:offset        将文件的长度设置为offset字节
 
 

NSFileManager和NSFileHandle使用的更多相关文章

  1. IOS之NSFileManager 和NSFileHandle

    在现阶手机app的临时缓存文件渐渐增多,在app开发中对于移动设备文件的操作越来越多,我们IOS中对于文件的操作主要涉及两个类NSFileManager 和NSFileHandle,下面我们就看看如何 ...

  2. ios NSFileManager和NSFileHandle(附:获取文件大小 )

    转自 http://blog.csdn.net/zhibudefeng/article/details/7795946 //file 文件操作 NSFileManager  常见的NSFileMana ...

  3. OC语言--NSFileManager& NSFileHandle

    1.关于文件的介绍 ->什么是文件: 文件概念, 广义文件. 狭义文件(磁盘文件). 文件常见的使用操作(可用命令行演示文件操作的使用场景). ->什么是路径: 简单来说就是,在系统中,要 ...

  4. 高级UIKit-03(NSFileManager、NSFileHandle)

    fileManager文件管理器 [day04_1_FileManager_Search] :查找文件 fileManager有一个方法可以判断文件是否是文件夹, fileExistsAtPath:i ...

  5. IOS文件系统及其相关操作(NSFileManager,NSFileHandle)

    How do you get the paths to these special sandbox directories? NSArray *NSSearchPathForDirectoriesIn ...

  6. NSFileManager和NSFileHandle(附:获取文件大小 )

    本文转载至:http://www.cnblogs.com/pengyingh/articles/2350345.html 天牛 感谢原创作者的硕果 //file 文件操作 NSFileManager  ...

  7. objective-c常用方法列表(总结)

    第1章 Objective-C学习环境准备 1.1 Objective-C基础 1.1.1 Objective-C的发展历程 1.1.2 Objective-C语言的特点 1.1.3 技术架构 1.2 ...

  8. iOS - OC 数据持久化

    1.Sandbox 沙箱 iOS 为每个应用提供了独立的文件空间,一个应用只能直接访问为本应用分配的文件目录,不可以访问其他目录,每个应用自己独立的访问空间被称为该应用的沙盒.也就是说,一个应用与文件 ...

  9. iOS - Swift 数据持久化

    1.Sandbox 沙箱 iOS 为每个应用提供了独立的文件空间,一个应用只能直接访问为本应用分配的文件目录,不可以访问其他目录,每个应用自己独立的访问空间被称为该应用的沙盒.也就是说,一个应用与文件 ...

随机推荐

  1. review14

    日期的格式化 Formatter类的format方法: format(格式化模式, 日期列表) 按着“格式化模式”返回“日期列表”中所列各个日期中所含数据(年,月,日,小时等数据)的字符串表示.“格式 ...

  2. js轮训

    Later.js,一个独立的JavaScript类库,提供了循环事件触发的高级应用,可以为项目替换以上两种方法. 通常,我们要完成复杂的时间预定,需要大量的if...else语句.Later.js提供 ...

  3. IIS7 配置PHP服务器

    安装PHP Manager: 1)访问 http://phpmanager.codeplex.com/releases/view/69115 下载PHP Manager.其中,x86 为32位 Win ...

  4. Alfred 2

    Alfred https://www.alfredapp.com/ Alfred Workflow https://www.alfredapp.com/workflows/ http://www.pa ...

  5. mpv 播放器

    https://mpv.io/ Mac版本: 官网: https://mpv.io/ https://mpv.io/installation/ 下载: http://sva.wakku.to/~chr ...

  6. ural 2018 The Debut Album(dp¥)

    2018. The Debut Album Time limit: 2.0 secondMemory limit: 64 MB Pop-group “Pink elephant” entered on ...

  7. Element 'beans' cannot have character [children], because the type's content type is element-only

    这个小问题快搞死我了,找了大半个小时. Element 'beans' cannot have character [children], because the type's content typ ...

  8. 【Prism】MEF版HelloWorld

    引言 Pirsm框架是由微软P & P小组设计的,用于构建组合式的WPF企业级应用,支持两个IOC容器,分别为Unity和MEF.官方地址为http://compositewpf.codepl ...

  9. js操作获取和设置cookie

    //创建cookie function setCookie(name, value, expires, path, domain, secure) { var cookieText = encodeU ...

  10. 不同OpenCV版本和不同VS版本之间进行配置的注意事项

    下面内容为不同系统和不同版本VS+不同版本OpenCV之间进行配置时的注意事项.本教程中开始提到如果VS版本和OpenCV版本相匹配的话,只要按上述步骤配置都是没有问题的.但是如果说版本不匹配的话,就 ...