IOS开发-UI学习-沙盒机制&文件操作
2016-03-30 11:41
585 查看
苹果为软件的运行提供了一个沙盒机制
每个沙盒含有3个文件夹:Documents, Library 和 tmp。因为应用的沙盒机制,应用只能在几个目录下读写文件
Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;
Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除;
tmp:提供一个即时创建临时文件的地方,
这三个文件夹如图:
对于Documents和Library可以有几种方式去获得它的路径,而tmp只有一种方式。这些获取路径的方式大多是C语言中的文件操作函数。
在程序文件夹中创建一个新的文件夹的操作;
新建完成后在Documents下面多了一个文件夹New。
创建文件new.mp3:
删除文件new.mp3
每个沙盒含有3个文件夹:Documents, Library 和 tmp。因为应用的沙盒机制,应用只能在几个目录下读写文件
Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;
Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除;
tmp:提供一个即时创建临时文件的地方,
这三个文件夹如图:
对于Documents和Library可以有几种方式去获得它的路径,而tmp只有一种方式。这些获取路径的方式大多是C语言中的文件操作函数。
// 获得并打印本app的沙盒,里面有三个文件夹,doucment,library,tmp NSString * homepath = [NSString stringWithFormat:@"%@",NSHomeDirectory()]; NSLog(@"%@",homepath); // 获得并打印document路径的第一种方法 NSString *documentPath = [NSString stringWithFormat:@"%@/Documents",NSHomeDirectory()]; NSLog(@"%@",documentPath); // 获得并打印document路径的第二种方法,拿到的是一个数组,而document路径是这个数组的第一个元素。 NSArray *documentPath2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSLog(@"%@",[documentPath2 objectAtIndex:0]); // 获得并打印caches路径的方法1: NSArray *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSLog(@"%@",cachePath[0]); // 获得并打印cache路径的方法2: NSString *cachePath2 = [NSString stringWithFormat:@"%@/Library/Caches",NSHomeDirectory()]; NSLog(@"%@",cachePath2); // 获取并打印tmp路径的方法 NSString *tmpPath = NSTemporaryDirectory(); NSLog(@"%@",tmpPath);
在程序文件夹中创建一个新的文件夹的操作;
// 在document下面创建一个文件夹 // 先创建一个路径 NSString *newPath = [NSString stringWithFormat:@"%@/Documents/New",NSHomeDirectory()]; NSLog(@"%@",newPath); // 再使用之前创建的路径使用NsFileManager去创建一个文件夹,yesOrNo返回是否创建成功的消息 BOOL yesOrNo = [[NSFileManager defaultManager]createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes:nil error:nil];
新建完成后在Documents下面多了一个文件夹New。
创建文件new.mp3:
// 创建文件 // 先把文件路径和文件名定义好 NSString *newfile = [NSString stringWithFormat:@"%@/new.mp3",newPath]; // 使用createFileAtPath创建文件 [[NSFileManager defaultManager]createFileAtPath:newfile contents:nil attributes:nil];
删除文件new.mp3
// 删除文件 [[NSFileManager defaultManager]removeItemAtPath:newfile error:nil];
相关文章推荐
- GUI 初体验与事件处理实战
- iOS UITextField或UITextView的内容是否为空
- xib系列---基本 UI 控件详解 (UISegmentedControl | UIImageView | UIProgressView | UISlider | UIAlertView )
- HiveJDBCInterface Integration with Pentaho or SQuirrel SQL Client
- UIlabel 设置圆角 自定义颜色 字体居中
- Error:Execution failed for ..dk\build-tools\23.0.2\aapt.exe'' finished with non-zero exit value 1错误
- TypedValue解析
- java中String、StringBuffer、StringBuilder的区别
- 软件版本GA,RC,alpha,beta,Build 含义
- ActionContext 、ValueStack 、Stack Context
- js 实现动态key value(JSON字符串注意事项:key和value都要用双引号,官网指定用双引号)
- 浅析 c# Queue
- requests抓取需登录(cookie)的页面
- android graphic(19)—why Android's UI was designed the way it is and how it actually works
- LeetCode OJ 334. Increasing Triplet Subsequence
- SonarQube-Java规范之Atomic-".equals()" should not be used to test the values of "Atomic" classes
- android graphic(18)—How about some Android graphics true facts?
- 设计模式 - Builder模式
- 用marquee和div+js实现首尾相连循环滚动效果
- GTK+ and Glade3 GUI Programming