iOS沙盒机制及文件操作 NSFileManager的基本使用

本人借鉴前人经验,自己总结了一点沙盒的相关知识和NSFileManager的基本使用


一 、沙盒

 iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等。

 1.1、每个应用程序都有自己的存储空间
 1.2、应用程序不能翻过自己的围墙去访问别的存储空间的内容
 1.3、应用程序请求的数据都要通过权限检测,假如不符合条件的话,不会被放行。


打开模拟器沙盒的方法:1.打开资源库后找到/Application Support/iPhone Simulator/文件夹。这里面就是模拟器的各个程序的沙盒目录了;2.

在Finder上点->前往->前往文件夹,输入/Users/你的用户名/Library/Application Support/iPhone Simulator/  前往。


沙盒里的目录结构


默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。因为应用的沙盒机制,应用只能在几个目录下读写文件
Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;

Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除

tmp:提供一个即时创建临时文件的地方。


iTunes在与iPhone同步时,备份所有的Documents和Library文件。

iPhone在重启时,会丢弃所有的tmp文件。


下面是沙盒各目录的获取 及文件操作的代码


<pre name="code" class="objc">//沙盒主目录
    NSString *homePath = NSHomeDirectory();
    NSLog(@"homePath,%@",homePath);
    
    //Document目录
    NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSMutableString *documentPath = [documentArray objectAtIndex:0];
    NSArray *testArray = [NSArray arrayWithObjects:@"hello , world",@"你好,世界" ,nil];
    [testArray writeToFile:[documentPath stringByAppendingPathComponent:@"document1.txt"]atomically:YES];
    NSLog(@"documentPath,%@",documentPath);
    
    //Library目录
    NSArray *library = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryPath = [library objectAtIndex:0];
    NSLog(@"libraryPath,%@",libraryPath);
    [testArray writeToFile:[libraryPath stringByAppendingPathComponent:@"document2.txt"]atomically:YES];
    
    //Library->cache目录
    NSArray *cacheArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES);
    NSString *cachePath = [cacheArray objectAtIndex:0];
    [testArray writeToFile:[cachePath stringByAppendingPathComponent:@"document3.txt"]atomically:YES];
    NSLog(@"cachePath,%@",cachePath);
    
    
    //写文件到指定路径
    NSString *tempPath = NSTemporaryDirectory();
    NSLog(@"tempPath,%@",tempPath);
    [testArray writeToFile:[tempPath stringByAppendingPathComponent:@"document4.txt"]atomically:YES];
    
    //从指定路径读取文件
    
    NSArray *cacheFileArray = [[NSArray alloc]initWithContentsOfFile:[cachePath stringByAppendingPathComponent:@"document3.txt"]];
    NSLog(@"cacheFileArray , %@",cacheFileArray);
    

 
接下来是NSManager 对文件的基本操作代码(接上)
//NSFileManager 管理文件
    //创建文件夹
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *fileDirectoryPath = [documentPath stringByAppendingPathComponent:@"test"];
    [fileManager createDirectoryAtPath:fileDirectoryPath withIntermediateDirectories:YES attributes:nil error:nil];
    //创建文件
    NSString *testStr = @"10万个why?";
    [fileManager createFileAtPath:[fileDirectoryPath stringByAppendingPathComponent:@"why.txt"] contents:[testStr dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    
    [fileManager createFileAtPath:[fileDirectoryPath stringByAppendingPathComponent:@"why1.txt"] contents:[testStr dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    
    [fileManager createFileAtPath:[fileDirectoryPath stringByAppendingPathComponent:@"why2.txt"] contents:[testStr dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    
    //获取指定文件夹里的所有文件名
    
    NSArray *fileNamesArray = [fileManager subpathsOfDirectoryAtPath:fileDirectoryPath error:nil];
    NSLog(@"方法一:test 文件夹里的文件名:%@",fileNamesArray);
    
    fileNamesArray = [fileManager subpathsAtPath:fileDirectoryPath];
    
    NSLog(@"方法二:test 文件夹里的文件名:%@",fileNamesArray);
    
    //更改到当前目录
    [fileManager changeCurrentDirectoryPath:libraryPath];
    NSString *fileName = @"why.txt";
    NSArray *currentArray = [[NSArray alloc] initWithObjects:@"current1",@"current2",@"current3", nil];
    [fileManager createFileAtPath:fileName contents:currentArray attributes:nil];
    
    //删除当前目录下的某个文件
    
    [fileManager removeItemAtPath:fileName error:nil];
    
    
    //混合写数据
    NSMutableData *myData = [[NSMutableData alloc] init];
    int dataInt = 6666;
    float dataFloat = 88.88;
    NSString *myStr = @"testString";
    
    [myData appendData:[myStr dataUsingEncoding:NSUTF8StringEncoding]];
    [myData appendBytes:&dataInt length:sizeof(dataInt)];
    [myData appendBytes:&dataFloat length:sizeof(dataFloat)];
    
    [myData writeToFile:[fileDirectoryPath stringByAppendingPathComponent:@"why.txt"] atomically:YES];
    
    //混合读数据
    
    int intData;
    float floatData = 0.0;
    NSString *strData;
    
    NSData *dataOut = [NSData dataWithContentsOfFile:[fileDirectoryPath stringByAppendingPathComponent:@"why.txt"]];
    
    
    strData = [[NSString alloc] initWithData:[dataOut subdataWithRange:NSMakeRange(0, [myStr length])] encoding:NSUTF8StringEncoding];
    [dataOut getBytes:&intData range:NSMakeRange([myStr length], sizeof(intData))];
    [dataOut getBytes:&floatData range:NSMakeRange([myStr length] + sizeof(intData),sizeof(floatData))];
    NSLog(@"strData:%@ ,intData:%d ,floatData:%f",strData,intData,floatData);
    



以下是打印结果

homePath,/Users/qxl/Library/Developer/CoreSimulator/Devices/795EDA73-3458-4FE5-AE6A-C44CB1DABA3A/data/Containers/Data/Application/E02A7561-4F06-4EE9-A2F5-1B5D63C11AC8


documentPath,/Users/qxl/Library/Developer/CoreSimulator/Devices/795EDA73-3458-4FE5-AE6A-C44CB1DABA3A/data/Containers/Data/Application/E02A7561-4F06-4EE9-A2F5-1B5D63C11AC8/Documents


libraryPath,/Users/qxl/Library/Developer/CoreSimulator/Devices/795EDA73-3458-4FE5-AE6A-C44CB1DABA3A/data/Containers/Data/Application/E02A7561-4F06-4EE9-A2F5-1B5D63C11AC8/Library


cachePath,/Users/qxl/Library/Developer/CoreSimulator/Devices/795EDA73-3458-4FE5-AE6A-C44CB1DABA3A/data/Containers/Data/Application/E02A7561-4F06-4EE9-A2F5-1B5D63C11AC8/Library/Caches


tempPath,/Users/qxl/Library/Developer/CoreSimulator/Devices/795EDA73-3458-4FE5-AE6A-C44CB1DABA3A/data/Containers/Data/Application/E02A7561-4F06-4EE9-A2F5-1B5D63C11AC8/tmp/


cacheFileArray , (

    "hello , world",

    "\U4f60\U597d\Uff0c\U4e16\U754c"

)


方法一:test 文件夹里的文件名:(

    "why.txt",

    "why1.txt",

    "why2.txt"

)


方法二:test 文件夹里的文件名:(

    "why.txt",

    "why1.txt",

    "why2.txt"

)


strData:testString ,intData:6666 ,floatData:88.879997





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值