zoukankan      html  css  js  c++  java
  • NSFileManager的简单介绍,在沙盒目录下对文件进行增删改查

    ios应用程序中所产生的所有资源和数据都存放在它的沙盒目录下,沙盒目录中主要包含三个文件夹,

    沙盒路径的获取:

    NSLog(@"%@",NSHomeDirectory());//打印出相关路径,在前往文件夹下达到

    Documents:将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录

    //到达Documents文件夹下
      NSArray *storeFile = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *docunmentDirectory = [storeFile objectAtIndex:0];

    Library:

              Caches:存放缓存文件,保持数据的持久化

              Preferences:偏好设置

     //到达Library目录下
        NSArray *librarypaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *libraryDirectory = [librarypaths objectAtIndex:0];
    //到达缓存目录下
        NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *cachePath = [cache objectAtIndex:0];

     tmp:存放零时文件,重启时删除

    //到达tmp目录下
        NSString *tmpDirectory = NSTemporaryDirectory();

       

    NSFileManager的使用:

    1.使用NSFileManager创建文件,在沙盒路径下创建一个想要的文件和文件夹
    
    

           


    //
    初始化NSFileManager

    NSFileManager *manager = [NSFileManager defaultManager];
       //创建文件相关路径
       NSString *docunmentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];  
       NSString *filepath = [docunmentDirectory stringByAppendingPathComponent:@"moxue.plist"];
       //创建文件(在document文件夹下创建)

    [manager createFileAtPath:filepath contents:nil attributes:nil];
        //创建文件夹
        NSString *documentpath = [docunmentDirectory stringByAppendingPathComponent:@"moxue"]; 
    [manager createDirectoryAtPath:documentpath attributes:nil];
        //使用NSFileManager创建文件
        //创建文件的属性为attributes,可通过attributesOfItemAtPath中的属性列表查看,对attributes传入相关的字典就可以更改了。
        //通过attributesOfItemAtPath中的属性列表查看
        //    NSLog(@"%@",[manager attributesOfItemAtPath:filepath error:nil]);

    2.通过字符串来创建文件,并将字符串写入相应的文件中

    //通过字符串来创建文件,将字符串word写入filepath2文件路径中
        NSString *word = @"墨雪";
        NSString *filepath2 = [NSHomeDirectory() stringByAppendingPathComponent:@"moxue1"];
        [word writeToFile:filepath2 atomically:YES encoding:NSUTF8StringEncoding error:nil];

    3.在沙盒下移动文件

    //移动文件
        NSString *destation = [[NSHomeDirectory() stringByAppendingPathComponent:@"moxue"] stringByAppendingPathComponent:@"moxue2.plist"];
        [manager moveItemAtPath:filepath2 toPath:destation error:nil];

    4.沙盒下拷贝文件

    //拷贝文件
        NSString *copypath = [[NSHomeDirectory() stringByAppendingPathComponent:@"moxue"]stringByAppendingPathComponent:@"copy.plist"];
        [manager copyItemAtPath:destation toPath:copypath error:nil];

    5.在沙盒下删除文件

     //删除文件
        [manager removeItemAtPath:copypath error:nil];

    6.在沙盒下查找文件

    //查找文件,得到一个数组,得到数组中所有的数据
        NSArray *array = [manager contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"moxue"] error:nil];
        NSLog(@"array = %@",array);

     7.在沙盒下写入数据

        //在文件中写入数据
        NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"dsada122" forKey:@"item12"];
        [dict2 writeToFile:destation atomically:YES];
        NSDictionary *dict22 = [NSDictionary dictionaryWithContentsOfFile:destation];
  • 相关阅读:
    (Java实现) 洛谷 P1603 斯诺登的密码
    (Java实现) 洛谷 P1036 选数
    (Java实现) 洛谷 P1036 选数
    (Java实现) 洛谷 P1012 拼数
    (Java实现) 洛谷 P1012 拼数
    (Java实现) 洛谷 P1028 数的计算
    (Java实现) 洛谷 P1028 数的计算
    (Java实现) 洛谷 P1553 数字反转(升级版)
    8.4 确定两个日期之间的月份数或年数
    (Java实现) 洛谷 P1553 数字反转(升级版)
  • 原文地址:https://www.cnblogs.com/moxuexiaotong/p/4918962.html
Copyright © 2011-2022 走看看