• ios NSFileManager创建目录、文件


    前言:接上篇博客,把对象写入文件,存入内存。那么,我们一般把需要保存的文件存放在什么位置了?沙盒中的Documetnts!

    正文:先附上自己的代码

    NSFileManager *fileManager = [NSFileManager defaultManager];        
    NSString *str1 = NSHomeDirectory();
    _filePath = [[NSString stringWithFormat:@"%@/Documents/imageViews/test.plist",str1]retain];
            
     NSLog(@"%@",_filePath);
      if(![fileManager fileExistsAtPath:_filePath]){//如果不存在,则说明是第一次运行这个程序,那么建立这个文件夹
        NSLog(@"first run");
          NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
          NSString *directryPath = [path stringByAppendingPathComponent:@"imageViews"];
           [fileManager createDirectoryAtPath:directryPath withIntermediateDirectories:YES attributes:nil error:nil];
           NSString *filePath = [directryPath stringByAppendingPathComponent:@"test.plist"];
          NSLog(@"%@",filePath);
          [fileManager createFileAtPath:filePath contents:nil attributes:nil];
    .........
     }

    此段代码创建的filePath为

    /Users/yuqiu/Library/Application Support/iPhone Simulator/6.1/Applications/2A8EFE9E-4CCA-41D1-8A5F-1FF00A115FA3/Documents/imageViews/test.plist
     

    一、创建~/Documents/imageViews/test.plist的详细步骤

         1、找到Documetnts目录所在的位置  

    NSString *str1 = NSHomeDirectory();

          str1为/Users/yuqiu/Library/Application Support/iPhone Simulator/6.1/Applications/2A8EFE9E-4CCA-41D1-8A5F-1FF00A115FA3

        2、加上Documetnts这层目录

     NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

    path为/Users/yuqiu/Library/Application Support/iPhone Simulator/6.1/Applications/2A8EFE9E-4CCA-41D1-8A5F-1FF00A115FA3/Documents
    注意:使用的是stringByAppendingPathComponent:方法,不需要加“/";  如果用的是别的方法加的话,需要写成@"/Documents".

    ps:这里1、2两步可以简化为

    NSArray *paths = NSSearchPathForDerictoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path = [paths lastObject];

        3、在Documetnts目录中创建名为imageViews的目录

    NSFileManager *fileManager = [NSFileManager defaultManager];      
    NSString *directryPath = [path stringByAppendingPathComponent:@"imageViews"];
    [fileManager createDirectoryAtPath:directryPath withIntermediateDirectories:YES attributes:nil error:nil];

    创建NSFileManager的实例fileManager.NSFileManager这个类是专门进行文件管理的,可以创建文件,目录,删除,遍历目录等。我们调用了 createDirectoryAtPath:方法创建了一个新目录。

        4、在imageViews目录里,创建文件test.plist.

    NSString *filePath = [directryPath stringByAppendingPathComponent:@"test.plist"];
    [fileManager createFileAtPath:filePath contents:nil attributes:nil];

       调用createFileAtPath:创建文件
       最后得到的整个文件路径为:

    二、NSFileManager常用的文件管理操作

         1、创建目录  createDirectoryAtPath:

         2、创建文件  createFileAtPath:

         3、删除某个文件  removeItemAtPath:

         4、检查某个文件是否存在  fileExistsAtPath: 

         5、检查文件是否可读 isReadableFileAtPath:

              是否可写:isWritableFileAtPath:

          6、取得文件属性  fileAttributesAtPath:

              改变文件属性changeAttributesAtPath:

          7、从path代表的文件中读取数据:contentsAtPath

          8、移动文件movePath:toPath:handler:

          9、复制文件copyPath:toPath:handler:

  • 相关阅读:
    Object类中常见的方法,为什么wait notify会放在Object里边
    什么是Java序列化和反序列化,如何实现Java序列化
    hashCode和equals方法的区别与联系
    MVC设计思想
    dstat命令--检查linux系统性能
    linux mint19.1解决网易云音乐安装后打不开的问题
    linux 修改用户密码
    MD5加密
    xmpp
    cocoapods安装
  • 原文地址:https://www.cnblogs.com/wyqfighting/p/3182224.html
Copyright © 2020-2023  润新知