【转】iOS学习笔记(十七)——文件操作(NSFileManager)
iOS的沙盒机制,应用只能访问自己应用目录下的文件。iOS不像android,没有SD卡概念,不能直接访问图像、视频等内容。iOS应用产生的内容,如图像、文件、缓存内容等都必须存储在自己的沙盒内。默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。Library包含Caches、Preferences目录。
上面的完整路径为:用户->资源库->Application Support->iPhone Simulator->6.1->Aplications
Documents:苹果建议将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;
Library/Caches:存放缓存文件,保存应用的持久化数据,用于应用升级或者应用关闭后的数据保存,不会被itunes同步,所以为了减少同步的时间,可以考虑将一些比较大的文件而又不需要备份的文件放到这个目录下。
tmp:提供一个即时创建临时文件的地方,但不需要持久化,在应用关闭后,该目录下的数据将删除,也可能系统在程序不运行的时候清除。
APP Sandbox
iOS怎么获取沙盒路径,怎么操作文件呢?下面给出答案。
获取应用沙盒根路径:
- -(void)dirHome{
- NSString *dirHome=NSHomeDirectory();
- NSLog(@"app_home: %@",dirHome);
- }
获取Documents目录路径:
- //获取Documents目录
- -(NSString *)dirDoc{
- //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSLog(@"app_home_doc: %@",documentsDirectory);
- return documentsDirectory;
- }
获取Library目录路径:
- //获取Library目录
- -(void)dirLib{
- //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
- NSString *libraryDirectory = [paths objectAtIndex:0];
- NSLog(@"app_home_lib: %@",libraryDirectory);
- }
获取Cache目录路径:
- //获取Cache目录
- -(void)dirCache{
- NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSString *cachePath = [cacPath objectAtIndex:0];
- NSLog(@"app_home_lib_cache: %@",cachePath);
- }
获取Tmp目录路径:
- //获取Tmp目录
- -(void)dirTmp{
- //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
- NSString *tmpDirectory = NSTemporaryDirectory();
- NSLog(@"app_home_tmp: %@",tmpDirectory);
- }
创建文件夹:
- //创建文件夹
- -(void *)createDir{
- NSString *documentsPath =[self dirDoc];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- // 创建目录
- BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
- if (res) {
- NSLog(@"文件夹创建成功");
- }else
- NSLog(@"文件夹创建失败");
- }
创建文件
- //创建文件
- -(void *)createFile{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];
- if (res) {
- NSLog(@"文件创建成功: %@" ,testPath);
- }else
- NSLog(@"文件创建失败");
- }
写数据到文件:
- //写文件
- -(void)writeFile{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- NSString *content=@"测试写入内容!";
- BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
- if (res) {
- NSLog(@"文件写入成功");
- }else
- NSLog(@"文件写入失败");
- }
读文件数据:
- //读文件
- -(void)readFile{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- // NSData *data = [NSData dataWithContentsOfFile:testPath];
- // NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
- NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
- NSLog(@"文件读取成功: %@",content);
- }
文件属性:
- //文件属性
- -(void)fileAttriutes{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];
- NSArray *keys;
- id key, value;
- keys = [fileAttributes allKeys];
- int count = [keys count];
- for (int i = 0; i < count; i++)
- {
- key = [keys objectAtIndex: i];
- value = [fileAttributes objectForKey: key];
- NSLog (@"Key: %@ for value: %@", key, value);
- }
- }
删除文件:
- //删除文件
- -(void)deleteFile{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- BOOL res=[fileManager removeItemAtPath:testPath error:nil];
- if (res) {
- NSLog(@"文件删除成功");
- }else
- NSLog(@"文件删除失败");
- NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");
- }
【转】iOS学习笔记(十七)——文件操作(NSFileManager)的更多相关文章
- IOS学习笔记25—HTTP操作之ASIHTTPRequest
IOS学习笔记25—HTTP操作之ASIHTTPRequest 分类: iOS2012-08-12 10:04 7734人阅读 评论(3) 收藏 举报 iosios5网络wrapper框架新浪微博 A ...
- node 学习笔记 - fs 文件操作
本文同步自我的个人博客:http://www.52cik.com/2015/12/03/learn-node-fs.html 最近看到群里不少大神都开始玩 node 了,我感觉跟他们步伐越来越大了, ...
- node学习笔记3——文件操作fs
文件操作关键字: http('fs') —— 请求 node 里面的 http 模块 readFile —— 读文件,参数包括 文件名,回调函数 writeFile —— 写文件,参数包括 文件 ...
- python学习笔记(三):文件操作和集合
对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 文件基本操作: f = open('file.txt','r') #以只读方式打开一个 ...
- C++ 学习笔记之——文件操作和文件流
1. 文件的概念 对于用户来说,常用到的文件有两大类:程序文件和数据文件.而根据文件中数据的组织方式,则可以将文件分为 ASCII 文件和二进制文件. ASCII 文件,又称字符文件或者文本文件,它的 ...
- Java 学习笔记(14)—— 文件操作
java文件操作主要封装在Java.io.File中,而文件读写一般采用的是流的方式,Java流封装在 java.io 包中.Java中流可以理解为一个有序的字符序列,从一端导向到另一端.建立了一个流 ...
- python学习笔记之文件操作(三)
这篇博客小波主要介绍一下python对文件的操作 对文件的操作主要分为三步: 1.打开文件获取文件的句柄,句柄也是文件描述符 2.通过文件句柄操作文件 3.关闭文件. 现有以下文件,是小波随写的周杰伦 ...
- python学习笔记4(文件操作)
文件操作: 1.f=open(”caidan”,”w”,encoding=”utf8”) 直接打开一个文件,如果文件不存在则创建文件 f.close() 2.with open (”caid ...
- python学习笔记三 文件操作(基础篇)
文件操作 打开文件 open(name[,mode[,buffering]]) open函数使用一个文件名作为强制参数,然后返回一个文件对象.[python 3.5 把file()删除掉] w ...
- 【java学习笔记】文件操作
文件操作 java.io.File ①创建删除文件及目录 ②查看文件及目录属性 ③文件过滤器 (PS:不包括文件读写数据) 1.单个文件 创建单个文件,查看属性,删除单个文件. package tmp ...
随机推荐
- CABasicAnimation动画及其keypath值和作用
//tarnsform放大缩小动画 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transf ...
- 网站特效离不开脚本,javascript是最常用的脚本语言,我们归纳一下常用的基础函数和语法:
转载自网络,非原创 1.输出语句:document.write(""); 2.JS中的注释为//3.传统的HTML文档顺序是:document->html->(head ...
- Intellij IDEA生成JavaDoc(转)
Intellij IDEA生成JavaDoc(转)
- 用js判断屏幕的宽度,改变html字体大小用rem布局
if (document.documentElement.clientWidth > 600) {//页面宽度大于600px让其宽度等于600px,字体大小等于60px,居中 document. ...
- spring-eureka 源码解读----为什么一个服务最多两分钟被其他服务感知
Eureka的wiki上有一句话,大意是一个服务启动后最长可能需要2分钟时间才能被其它服务感知到,但是文档并没有解释为什么会有这2分钟.其实这是由三处缓存 + 一处延迟造成的. 首先,Eureka对H ...
- Vue2中实现微信分享支付功能
Vue2中实现微信分享支付功能 近期做了一个微信公众号前后分离项目,前端采用Vue2开发,后端SpringBoot,今天迫不及待的来给大家分享一下这次在开发中遇到的一些坑以及解决办法. 在这里,一些 ...
- 支持通配符查询的k-gram索引
k-gram索引的通配符查询处理技术称为k-gram索引. 一个k-gram代表由k个字符组成的序列.对于词项castle来说,cas.ast.stl都是3-gram.我们用特殊的字符$来标识词项的开 ...
- docker网络设置(待整理)
手动指定容器的配置 -h HOSTNAME or --hostname=HOSTNAME \\设定容器的主机名. --dns=IP_ADDRESS \\指定DNS地址. ...
- ASP.NET Core Linux
环境说明 CentOS / 7.1 (64bit) (Linux操作系统) 3MySQL5.7(网站应用数据库) .NET Core SDK 2.0.0(网站应用环境) Nginx(反向代理服务器) ...
- Core 事件总
NET Core 事件总线,分布式事务解决方案:CAP 背景 相信前面几篇关于微服务的文章也介绍了那么多了,在构建微服务的过程中确实需要这么一个东西,即便不是在构建微服务,那么在构建分布式应用的过程中 ...