Object-C 学习笔记(二十七)--- 文件操作(沙盒)

来源:互联网 发布:淘宝一直显示等待揽收 编辑:程序博客网 时间:2024/05/23 11:59

沙盒: 

--- 每个iOS应用SDK都被包含在"沙盒"中,"沙盒"相当于一个加了仅主人可见权限的文件夹,苹果对沙盒有以下几条限制。

    (1)、应用程序可以在自己的沙盒里运作,但是不能访问任何其他应用程序的沙盒。

    (2)、应用程序间不能共享数据,沙盒里的文件不能被复制到其他应用程序文件夹中,也不能把其他应用程序文件夹中的文件复制到沙盒里。

    (3)、苹果禁止任何读、写沙盒以外的文件,禁止应用程序将内容写到沙盒以外的文件夹中。

    (4)、沙盒根目录里有三个文件夹:

Documents,一般应该把应用程序的数据文件存到这个文件夹里,用于存储用户数据或其他应该定期备份的信息。

Library,下有两个文件夹,Caches存储应用程序再次启动所需的信息,Preferences包含应用程序偏好设置文件,不过不要在这里修改偏好设置。

temp,存放临时文件,即应用程序再次启动不需要的文件。


下面实例是对沙盒路径上面的一些文件操作:建立一个application的空工程,代码如下:

////  AppDelegate.m//  Filec////  Created by 5016 on 13-12-13.//  Copyright (c) 2013年 dradon. All rights reserved.//#import "AppDelegate.h"@implementation AppDelegate- (void)dealloc{    [_window release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];        //获取跟目录    NSString *homePath = NSHomeDirectory();    NSLog(@"homePath=%@",homePath);    //获取document路径,拼接    NSString *docPath = [homePath stringByAppendingPathComponent:@"documents"];    NSLog(@"docPath=%@",docPath);    NSString *docPath1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES)objectAtIndex:0];    NSLog(@"docPath1 = %@",docPath1);        //获取Library文件夹    NSString *libPath = [homePath stringByAppendingPathComponent:@"Library"];    NSLog(@"libPath=%@",libPath);    NSString *libPath1 = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask,YES)objectAtIndex:0];    NSLog(@"libPath1 = %@",libPath1);        //获取Caches路径    NSString *cachesPath = [homePath stringByAppendingPathComponent:@"Library/Caches"];    NSLog(@"cachePath=%@",cachesPath);    NSString *cachesPath1 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES)objectAtIndex:0];    NSLog(@"cachePath1 = %@",cachesPath1);        //获取Preferences路径    NSString *prePath = [homePath stringByAppendingPathComponent:@"Library/Preferences"];    NSLog(@"prePath=%@",prePath);        //获取temp路径    NSString *tmpPath = NSTemporaryDirectory();    NSLog(@"tmp=%@",tmpPath);            /**************在沙盒路径里面创建文件*****************/    //1.找到想要创建的文件路径    NSString *helloPath = [docPath1 stringByAppendingPathComponent:@"hello.txt"];    //2.创建文件管理者对象(单例对象)    NSFileManager *fm = [NSFileManager defaultManager];    //3.判断刚才创建的文件路径下是否已经存在一个文件可以管理,如果存在就不用创建,如果不存在就创建一个    if(![fm fileExistsAtPath:helloPath]){        [fm createFileAtPath:helloPath contents:nil attributes:nil];          }else{        NSLog(@"该文件已经存在,不会再次创建");    }    //文件写入    NSString *str = @"HELLO DRAGON";    [str writeToFile:helloPath atomically:YES encoding:NSUTF8StringEncoding error:nil];    //读出来    NSData *data = [NSData dataWithContentsOfFile:helloPath];    NSString *strdata = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];    NSLog(@"strdata=%@",strdata);    //删除    NSError *error = nil;    if([fm fileExistsAtPath:helloPath]){        [fm removeItemAtPath:helloPath error:&error];        NSLog(@"error = %@",[error localizedDescription]);//错误原因    }    NSString *toPath = [libPath stringByAppendingPathComponent:@"hello.txt"];    //移动文件    if([fm fileExistsAtPath:helloPath]){        [fm moveItemAtPath:helloPath toPath:toPath error:&error];    }else{        NSLog(@"没找到移动的文件");    }        //查看某个目录下有多少个文件    NSArray *arr = [fm contentsOfDirectoryAtPath:libPath error:nil];    NSLog(@"arr = %@",arr);        //查找从外部拖入的文件NSBundle    NSString *abcPath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"txt"];    NSLog(@"abcPath = %@",abcPath);         /**************在沙盒路径里面创建文件*****************/        [self.window makeKeyAndVisible];    return YES;}@end





0 0
原创粉丝点击