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怎么获取沙盒路径,怎么操作文件呢?下面给出答案。

获取应用沙盒根路径:

  1. -(void)dirHome{
  2. NSString *dirHome=NSHomeDirectory();
  3. NSLog(@"app_home: %@",dirHome);
  4. }

获取Documents目录路径:

  1. //获取Documents目录
  2. -(NSString *)dirDoc{
  3. //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  4. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  5. NSString *documentsDirectory = [paths objectAtIndex:0];
  6. NSLog(@"app_home_doc: %@",documentsDirectory);
  7. return documentsDirectory;
  8. }

获取Library目录路径:

  1. //获取Library目录
  2. -(void)dirLib{
  3. //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
  4. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  5. NSString *libraryDirectory = [paths objectAtIndex:0];
  6. NSLog(@"app_home_lib: %@",libraryDirectory);
  7. }

获取Cache目录路径:

  1. //获取Cache目录
  2. -(void)dirCache{
  3. NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4. NSString *cachePath = [cacPath objectAtIndex:0];
  5. NSLog(@"app_home_lib_cache: %@",cachePath);
  6. }

获取Tmp目录路径:

  1. //获取Tmp目录
  2. -(void)dirTmp{
  3. //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
  4. NSString *tmpDirectory = NSTemporaryDirectory();
  5. NSLog(@"app_home_tmp: %@",tmpDirectory);
  6. }

创建文件夹:

  1. //创建文件夹
  2. -(void *)createDir{
  3. NSString *documentsPath =[self dirDoc];
  4. NSFileManager *fileManager = [NSFileManager defaultManager];
  5. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  6. // 创建目录
  7. BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
  8. if (res) {
  9. NSLog(@"文件夹创建成功");
  10. }else
  11. NSLog(@"文件夹创建失败");
  12. }

创建文件

  1. //创建文件
  2. -(void *)createFile{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSFileManager *fileManager = [NSFileManager defaultManager];
  6. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  7. BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];
  8. if (res) {
  9. NSLog(@"文件创建成功: %@" ,testPath);
  10. }else
  11. NSLog(@"文件创建失败");
  12. }

写数据到文件:

  1. //写文件
  2. -(void)writeFile{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  6. NSString *[email protected]"测试写入内容!";
  7. BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  8. if (res) {
  9. NSLog(@"文件写入成功");
  10. }else
  11. NSLog(@"文件写入失败");
  12. }

读文件数据:

  1. //读文件
  2. -(void)readFile{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  6. //    NSData *data = [NSData dataWithContentsOfFile:testPath];
  7. //    NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
  8. NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
  9. NSLog(@"文件读取成功: %@",content);
  10. }

文件属性:

  1. //文件属性
  2. -(void)fileAttriutes{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSFileManager *fileManager = [NSFileManager defaultManager];
  6. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  7. NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];
  8. NSArray *keys;
  9. id key, value;
  10. keys = [fileAttributes allKeys];
  11. int count = [keys count];
  12. for (int i = 0; i < count; i++)
  13. {
  14. key = [keys objectAtIndex: i];
  15. value = [fileAttributes objectForKey: key];
  16. NSLog (@"Key: %@ for value: %@", key, value);
  17. }
  18. }

删除文件:

  1. //删除文件
  2. -(void)deleteFile{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSFileManager *fileManager = [NSFileManager defaultManager];
  6. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  7. BOOL res=[fileManager removeItemAtPath:testPath error:nil];
  8. if (res) {
  9. NSLog(@"文件删除成功");
  10. }else
  11. NSLog(@"文件删除失败");
  12. NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath][email protected]"YES":@"NO");
  13. }
时间: 2024-08-27 22:26:51

iOS学习笔记——文件操作(NSFileManager)的相关文章

PHP学习笔记-文件操作1

转载请标明出处: http://blog.csdn.net/hai_qing_xu_kong/article/details/52294237 本文出自:[顾林海的博客] 前言 PHP支持文件上传功能,因此学习PHP文件的相关操作是必须的,这篇笔记会去记录PHP文件系统的相关知识. 文件打开与关闭 文件处理包括读取.关闭.重写等,例如,访问一个文件需要3步:打开文件.读写文件和关闭文件.其他的操作要么是包含在读写文件中(如显示内容.写入内容等),要么与文件自身的属性有关系(如文件遍历.文件改名等

&lt;学习笔记&gt;文件操作篇

文件操作用到的函数: #include <stdio.h> fopen(FILE* fp): 文件打开函数: 1.打开成功返回当前文件所处的地址值:2.打开失败返回一个空指针"NULL"; 常用方法:if((fp=fopen("file_name","mode"))==NULL)printf("can not open this file!");常用此方法打开文件. mode代表含义: r 为读打开一个文本文件,若

erlang学习笔记(文件操作)

参考这里和这里了解到的文件操作的模块有很多:kernel下有:file,stdlib下有:filelib,filename,file_sorter.(具体查看官方文档)

python学习笔记文件操作(六)

1.文件操作流程: 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 如下文件: 2017-03-24 11:25:06:349 - info: [debug] [AndroidBootstrap] Sending command to android: {"cmd":"shutdown"} 2017-03-24 11:25:06:355 - info: [debug] [AndroidBootstrap] Received command 

Python3.5学习笔记-文件操作

在Python中,操作文件对象使用open函数来创建,下表列出了常用的操作file的函数: 序号 方法及描述 1.file.close() 关闭文件.关闭后文件不能再进行读写操作. 2.file.flush() 刷新文件内部缓冲,直接把内部缓冲区的数据立刻写入文件, 而不是被动的等待输出缓冲区写入. 3.file.fileno() 返回一个整型的文件描述符(file descriptor FD 整型), 可以用在如os模块的read方法等一些底层操作上. 4.file.isatty() 如果文件

Python学习笔记——文件操作

python中,一切皆对象.   一.文件操作流程 (1)打开文件,得到一个文件句柄(对象),赋给一个对象: (2)通过文件句柄对文件进行操作: (3)关闭文件. 文件对象f通过open()函数来创建. open函数常用的三个参数:文件路径/名称.模式.encoding. 1 f = open('测试文件','r',encoding='utf-8') 2 print(f.read()) 3 f.close() 二.open函数中模式参数的常用值 值 描述 'r' 读模式 'w' 写模式 'a'

iOS学习笔记(十六)——数据库操作(使用FMDB)

iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepersistentobjects等,FMDB (https://github.com/ccgus/fmdb) 是一款简洁.易用的封装库,这一篇文章简单介绍下FMDB的使用. 在FMDB下载文件后,工程中必须导入如下文件,并使用 libsqlite3.dylib 依赖包. FMDB同时兼容ARC和非ARC工

iOS——文件操作NSFileManager (创建、删除,复制,粘贴)

iOS——文件操作NSFileManager (创建.删除,复制,粘贴) iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容.iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内.默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp.Library包含Caches.Preferences目录.               上面的完整路径为:用户->资源库->Applicat

EasyARM i.mx28学习笔记——文件IO方式操作GPIO

0 前言 本文描述如果通过文件IO sysfs方式控制EasyARM GPIO端口.通过sysfs方式控制GPIO,先访问/sys/class/gpio目录,向export文件写入GPIO编号,使得该GPIO的操作接口从内核空间暴露到用户空间,GPIO的操作接口包括direction和value等,direction控制GPIO方向,而value可控制GPIO输出或获得GPIO输入. Linux学习可从应用出发,先不纠结Linux驱动编写,先把Linux给玩起来. [相关博文] [EasyARM