iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储通常放在什么位置,获得模拟器的路径的简单方式是什么.app
documents,tmp,app,Library。iphone
(NSHomeDirectory()),函数
手动保存的文件在documents文件里spa
Nsuserdefaults保存的文件在tmp文件夹里.net
一、Documents 目录:您应该将全部de应用程序数据文件写入到这个目录下。这个目录用于存储用户数据或其它应该按期备份的信息。orm
二、AppName.app 目录:这是应用程序的程序包目录,包含应用程序的本身。因为应用程序必须通过签名,因此您在运行时不能对这个目录中的内容进行修改,不然可能会使应用程序没法启动。对象
三、Library 目录:这个目录下有两个子目录:Caches 和 Preferences
Preferences 目录:包含应用程序的偏好设置文件。您不该该直接建立偏好设置文件,而是应该使用NSUserDefaults类来取得和设置应用程序的偏好.
Caches 目录:用于存放应用程序专用的支持文件,保存应用程序再次启动过程当中须要的信息。blog
四、tmp 目录:这个目录用于存放临时文件,保存应用程序再次启动过程当中不须要的信息。图片
获取这些目录路径的方法:
1,获取家目录路径的函数:
NSString *homeDir = NSHomeDirectory();
2,获取Documents目录路径的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
3,获取Caches目录路径的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
4,获取tmp目录路径的方法:
NSString *tmpDir = NSTemporaryDirectory();
5,获取应用程序程序包中资源文件路径的方法:
例如获取程序包中一个图片资源(apple.png)路径的方法:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@”apple” ofType:@”png”];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
代码中的mainBundle类方法用于返回一个表明应用程序包的对象。ip
iphone沙盒(sandbox)中的几个目录获取方式:
[cpp] view plain copy
// 获取沙盒主目录路径
NSString *homeDir = NSHomeDirectory();
// 获取Documents目录路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
// 获取Caches目录路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
// 获取tmp目录路径
NSString *tmpDir = NSTemporaryDirectory();
[cpp] view plain copy
// 获取当前程序包中一个图片资源(apple.png)路径
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"png"];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
例子:
NSFileManager* fm=[NSFileManager defaultManager];
if(![fm fileExistsAtPath:[self dataFilePath]]){
//下面是对该文件进行制定路径保存
[fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil];
//取得一个目录下得全部文件名
NSArray *files = [fm subpathsAtPath: [self dataFilePath] ];
//读取某个文件
NSData *data = [fm contentsAtPath:[self dataFilePath]];
//或者NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]];}