在前面三篇关于数据持久化,我们都用涉及到文件(plist文件,数据库文件),它们都是把它们存储在document目录下。iOS的文件机制是沙盒机制,应用只能访问自己应用目录下的文件。iOS应用产生的内容,如图像、文件、缓存内容等都必须存储在自己的沙盒内。默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。Library包含Caches、Preferences目录。

Documents:苹果建议将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;
Library/Caches:存放缓存文件,保存应用的持久化数据,用于应用升级或者应用关闭后的数据保存,不会被itunes同步,所以为了减少同步的时间,可以考虑将一些比较大的文件而又不需要备份的文件放到这个目录下。
tmp:提供一个即时创建临时文件的地方,但不需要持久化,在应用关闭后,该目录下的数据将删除,也可能系统在程序不运行的时候清除。

我们实际中要如何来或取沙盒,如何在沙盒里面进行相应的操作,下面一一分解。

1.获取app的沙盒根目录

NSString *appRootDir=NSHomeDirectory();
NSLog(@"我的沙盒路径根路径------》: %@",appRootDir);

2.获取Documents目录路径

//第一种
NSString *appRootDir=NSHomeDirectory();
NSLog(@"我的沙盒路径根路径------》: %@",appRootDir);
NSString *documentDir = [appRootDir stringByAppendingPathComponent:@"Documents"];
NSLog(@"documentDir: -----》 %@",documentDir); //第二种
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"documentsDirectory: -----》 %@",documentsDirectory);

3.获取Library(包含Caches、Preferences)目录路径:

//第一种
NSString *appRootDir=NSHomeDirectory();
NSLog(@"我的沙盒路径根路径------》: %@",appRootDir);
NSString *documentDir = [appRootDir stringByAppendingPathComponent:@"Library"];
NSLog(@"documentDir: -----》 %@",documentDir); //第二种
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
NSLog(@"libraryDirectory: -----》 %@",libraryDirectory); //Caches
NSString *cachesPath = [libraryDirectory stringByAppendingString:@"Caches"]; //Preferences
NSString *preferencesPath = [libraryDirectory stringByAppendingString:@"Preferences"]; NSLog(@"cachesPath: -----》 %@",cachesPath);
NSLog(@"preferencesPath: -----》 %@",preferencesPath);

4.获取tmp目录路径:

//第一种
NSString *appRootDir=NSHomeDirectory();
NSLog(@"我的沙盒路径根路径------》: %@",appRootDir);
NSString *documentDir = [appRootDir stringByAppendingPathComponent:@"tmp"];
NSLog(@"documentDir: -----》 %@",documentDir); //第二种
NSString *tmpDirectory = NSTemporaryDirectory();
NSLog(@"tmpDirectory: -----》 %@",tmpDirectory);

5.创建文件(tmp文件夹中)

NSString *tmpDirectory = NSTemporaryDirectory();
NSLog(@"tmpDirectory: -----》 %@",tmpDirectory); NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testFilePath = [tmpDirectory stringByAppendingPathComponent:@"testFile.txt"];
BOOL res=[fileManager createFileAtPath:testFilePath contents:nil attributes:nil];
if (res) {
NSLog(@"测试文件创建成功: %@" ,testFilePath);
}else {
NSLog(@"测试文件创建失败");
}

6.创建文件夹

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testFolderDirectory = [documentsPath stringByAppendingPathComponent:@"Test"];
// 创建目录
BOOL res=[fileManager createDirectoryAtPath:testFolderDirectory withIntermediateDirectories:YES attributes:nil error:nil];
if (res) {
NSLog(@"Test文件夹创建成功");
}else {
NSLog(@"Test文件夹创建失败");
}

7.删除文件

NSString *tmpDirectory = NSTemporaryDirectory();
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testFilePath = [tmpDirectory stringByAppendingPathComponent:@"testFile.txt"]; //判断文件是否存在
if ([fileManager fileExistsAtPath:testFilePath]) {
BOOL res=[fileManager removeItemAtPath:testFilePath error:nil];
if (res) {
NSLog(@"testFile文件删除成功");
}else
NSLog(@"testFile文件删除失败");
}

8.写入文件

NSString *tmpDirectory = NSTemporaryDirectory();
NSLog(@"tmpDirectory: -----》 %@",tmpDirectory); NSString *content=@"www.babybus.com SuperDo";
NSString *testFilePath = [tmpDirectory stringByAppendingPathComponent:@"testFile.txt"];
BOOL res=[content writeToFile:testFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (res) {
NSLog(@"testFile文件写入成功");
}else {
NSLog(@"testFile文件写入失败");
}

以上是iOS 文件的一些简单常见操作。更多详细内容请参考(https://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW2

本站文章为宝宝巴士 SD.Team原创,转载务必在明显处注明:(作者官方网站:宝宝巴士
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4659923.html

[Objective-C] 013_文件系统(File System)的更多相关文章

  1. 在 Linux 中,最直观、最可见的部分就是 文件系统(file system)

    在 Linux 中,最直观.最可见的部分就是 文件系统(file system).下面我们就来一起探讨一下关于 Linux 中国的文件系统,系统调用以及文件系统实现背后的原理和思想.这些思想中有一些来 ...

  2. 磁盘、分区及Linux文件系统 [Disk, Partition, Linux File System]

    1.磁盘基础知识 1.1 物理结构 硬盘的物理结构一般由磁头与碟片.电动机.主控芯片与排线等部件组成:当主电动机带动碟片旋转时,副电动机带动一组(磁头)到相对应的碟片上并确定读取正面还是反面的碟面,磁 ...

  3. [CareerCup] 8.9 An In-memory File System 内存文件系统

    8.9 Explain the data structures and algorithms that you would use to design an in-memory file system ...

  4. KASS分布式文件系统(Kass File System)

    KASS分布式文件系统(Kass File System),简称KFS,是开始公司自主研发的分布式文件存储服务平台.KFS系统架构及功能服务类似Hadoop/GFS/DFS,它通过HTTP-WEB为上 ...

  5. NFS - Network File System网络文件系统

    NFS(Network File System/网络文件系统): 设置Linux系统之间的文件共享(Linux与Windows中间文件共享采用SAMBA服务): NFS只是一种文件系统,本身没有传输功 ...

  6. HTML5之本地文件系统API - File System API

    HTML5之本地文件系统API - File System API 新的HTML5标准给我们带来了大量的新特性和惊喜,例如,画图的画布Canvas,多媒体的audio和video等等.除了上面我们提到 ...

  7. File System 之本地文件系统

    上一篇文章提到了,最近做一个基于 File System/IndexedDB的应用,上一篇是定额和使用的查询. 因为LocalFileSystem只有chrome支持,有点尴尬,如果按需加载又何来尴尬 ...

  8. [LeetCode] Design In-Memory File System 设计内存文件系统

    Design an in-memory file system to simulate the following functions: ls: Given a path in string form ...

  9. 云服务器 linux文件系统异常an error occurren during the file system check导致服务器启动失败

    云服务器 linux文件系统异常an error occurren during the file system check导致服务器启动失败 文件系统宕机,重启后报错,无法启动 处理流程: 1.编辑 ...

随机推荐

  1. Automatic Reference Counting

    NSObject简化版alloc: struct obj_layout { NSUInteger retained; }; + (id)alloc { int size = sizeof(struct ...

  2. B - Housewife Wind POJ - 2763 树剖+边权转化成点权

    B - Housewife Wind POJ - 2763 因为树剖+线段树只能解决点权问题,所以这种题目给了边权的一般要转化成点权. 知道这个以后这个题目就很简单了. 怎么转化呢,就把这个边权转化为 ...

  3. Prime Path素数筛与BFS动态规划

    埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法.对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = ...

  4. 学习RxJava+Retrofit+OkHttp+MVP的网络请求使用

    公司的大佬用的是这一套,那我这个菜鸟肯定要学习使用了. 我在网上找了很多文章,写的都很详细,比如 https://www.jianshu.com/u/5fd2523645da https://www. ...

  5. QtCreator中使用链接库

    说明 之前讨论的DLL的静态链接和动态连接都是基于 MSVC 编译器,但是 MinGW 似乎有另外一套类似但是不相同的机制.下文均在 windows 下使用 Qt Creator 中使用 MinGW ...

  6. 【华为云技术分享】MongoDB经典故障系列五:sharding集群执行sh.stopBalancer()命令被卡住怎么办?

    [摘要] MongoDB sharding集群执行sh.stopBalancer()命令时被卡住怎么办?别慌,华为云数据库来给您支招,收下这份方案指南,让您分分钟远离被自建MongoDB数据库支配的恐 ...

  7. CSS之未知高度img垂直居中

    测试代码如下:(能够水平居中,通过text-align:center实现) <style>.box{ width:800px;height:600px;border:2px solid # ...

  8. python unittest TestCase间共享数据(全局变量的使用)

    文章目录 1.setupclass里设置self.xxx变量,不同用例之间无法实时共享参数变动 2.setupclass里设置globals().["xxx"]变量,不同用例之间可 ...

  9. vue 路由钩子。

    一.全局钩子 你可以使用 router.beforeEach 注册一个全局的 before 钩子: const router = new VueRouter({ ... }) router.befor ...

  10. 黑马程序员_毕向东_Java基础视频教程——逻辑运算符(随笔)

    逻辑运算符 逻辑运算符用于连接 boolean 型的表达式 & : 只要两边都是 boolean 表达结果,有一个为 false ,则结果就是 false 只要两边都为 true 则结果就为 ...