zoukankan      html  css  js  c++  java
  • ios NSFileManager创建目录、文件

    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:

  • 相关阅读:
    接口开发总结
    python多线程的坑
    ImageMagick 安装 window10与错误总结
    中文时间转换数字时间
    postgresql数据库中~和like和ilike的区别
    pdfplumber库解析pdf格式
    网络基础-数据通信过程
    渗透测试思路总述
    网络基础-OSI七层模型
    网络基础-常用网络测试工具
  • 原文地址:https://www.cnblogs.com/edensyd/p/9177724.html
Copyright © 2011-2022 走看看